Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Questions About switch Statements

$
0
0
Hello, so I'm making a blackjack game for fun. To make the cards I used this tutorial. I have all the cards assigned to a value with a swich statement like so.
public class Card {
	private int rank, suit;
	
	private static String[] suits = {"Diamonds", "Hearts", "Spades", "Clubs" };
	
	private static String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",
			"10", "Jack", "Queen", "King"};
	
	Card(int suit, int rank) {
		this.rank = rank;
		this.suit = suit;
	}
	
	public @Override String toString() {
		return ranks[rank] + " of " + suits[suit];
	}
	
	 public int getRank() {
	    	switch(rank) {
	    		case 0: 
	    			return 1;
	    		case 1: 
	    			return 2;
	    		case 2: 
	    			return 3;
	    		case 3: 
	    			return 4;
	    		case 4: 
	    			return 5;
	    		case 5: 
	    			return 6;
	    		case 6: 
	    			return 7;
	    		case 7: 
	    			return 8;
	    		case 8: 
	    			return 9;
	    		case 9: 
	    			return 10;
	    		case 10: 
	    			return 10;
	    		case 11: 
	    			return 10;
	    		case 12: 
	    			return 10;
	    		default: 
	    			return 0;
	    	}
	    }
	
	public int getSuit() {
		return suit;
	}
}

As you can see in case 0, it returns the ace card with a values of 1. However, ace's can be 1 or 11. I have tried to make a method to calculate the total amount the player has so if its greater than 21, check for an ace and assign it to a value of 0, however, I'm dumbfounded on a way to do it. Help is greatly appreciated. Thanks!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>