In the following code, each cards and suits have specific values except for the "Ace" card. I have the correct results when the computer randomly chooses anything except for the "Ace" card. When it does select the "Ace" card, I'm getting the wrong results.
This is correct, 3 + 9 + 7 = 19
run: Three of Spades, Nine of Hearts, Seven of Hearts, Player total: 19 BUILD SUCCESSFUL (total time: 0 seconds)
if playerTotal + 11 is < 22, I want to add 11 to playerTotal
if playerTotal + 11 is > 22, I want to add 1 to playerTotal, both of these doesn't seem to work, sometimes I get random values for the "Ace"
This is wrong because, 2 + 7 + 11 is not 16
run: Two of Spades, Seven of Hearts, Ace of Diamonds, Player total: 16 BUILD SUCCESSFUL (total time: 0 seconds)
package test; import java.util.Random; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Random rand = new Random(); String[] cards = {"Ace", "Two", "Three", "four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"}; String[] suits = {" of Hearts", " of Diamonds", " of Spades", " of Clubs"}; int shuffleCards = 0, shuffleSuits, playerPoints, playerTotal; playerTotal = 0; playerPoints = 0; for(int a=1; a<=3; a++) { shuffleCards = rand.nextInt(cards.length-1) + 0; shuffleSuits = rand.nextInt(suits.length-1) + 0; if(shuffleCards == 1) { playerPoints = 2; } if(shuffleCards == 2) { playerPoints = 3; } if(shuffleCards == 3) { playerPoints = 4; } if(shuffleCards == 4) { playerPoints = 5; } if(shuffleCards == 5) { playerPoints = 6; } if(shuffleCards == 6) { playerPoints = 7; } if(shuffleCards == 7) { playerPoints = 8; } if(shuffleCards == 8) { playerPoints = 9; } if(shuffleCards == 9 || shuffleCards == 10 || shuffleCards == 11 || shuffleCards == 12) { playerPoints = 10; } if((shuffleCards == 10 && shuffleSuits == 2 )|| (shuffleCards == 10 && shuffleSuits == 0) || (shuffleCards == 12 && shuffleSuits == 1)) { playerPoints = 13; } if(shuffleCards == 2 && shuffleSuits == 3) { playerPoints = 0; } System.out.print(cards[shuffleCards] + suits[shuffleSuits] +", "); playerTotal = playerTotal + playerPoints; } if(shuffleCards == 0 && (playerTotal + 11 < 22)) { playerTotal = playerTotal + 11; } if(shuffleCards == 0 && (playerTotal + 1 > 22)) { playerTotal = playerTotal + 1; } System.out.println(); System.out.println("Player total: " + playerTotal); } }