0

这个循环有问题(我认为)。当循环执行时,它会清除保存在变量 userHandValue 中的数据。我已经尝试了不同的循环来解决它,但我认为一切都很好,只是没有将抽到的新牌添加到手牌中。当你运行程序时,你会明白我在说什么。你可以从一手总点数达到 13 的牌开始,然后当你击球时,你的手牌总点数可以达到 10 点。我只是认为它没有按照应有的方式连接。

请记住,我是新来的,所以如果格式对你的口味来说太乱了,不要关闭我的帖子,请提供建议(帮助解决实际问题也很好)。谢谢你。

do {
 if (userHandValue < 21) {
     System.out.println("Would you like to hit? (Y/N)");
     hit = dataIn.readLine();
     hit = hit.toUpperCase();
 }
 again = hit.compareTo("Y") == 0;
 if (again) {
     // users next card draw
     randomGenNum = (int)((range * Math.random()) + 1)*2;
     // assigning the user hand value & card, pulling index from array
     tempUserHandValue = arrayCardValues[randomGenNum];
     // Check if the user was dealt an ace
     if ((tempUserHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5)) {
         tempUserHandValue = userHandValue + 10;
     }
     userCard = arrayCardSuites[randomGenNum];
     // displaying values to the screen
     System.out.println("Your next card is " + userCard);
     // adding new card to total hand value and displaying to user
     userHandValue = userHandValue + tempUserHandValue;
     System.out.println("Your new total hand value is: " + (userHandValue));
}
} while (again);

这是一切:

/*
 *This is a simple BlackJack game written by C. Auito and D. Harrington for CS241. 
 *Code snippets were taken from the following open source files:
 * http://www.dreamincode.net/forums/topic/75554-blackjack-game/
 * update test
 * 
 */
package blackjackclassproject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author C. Auito <chris.auito@gmail.com>
 */
public class BlackJackClassProject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

        //Declare Variables
        boolean again = true;
        //int again = 0;
        do {
            double range = 26;
            int randomGenNum;
            int kitty = 1000;
            int yourBet;
            int userHandValue = 0;
            int userDrawnValue;
            int dealerHandValue = 0;
            int dealerDrawnValue;
            String playAgain;
            String hit = "";
            String strBetAmount;
            String userCard, dealerCard;
            int[] arrayCardValues = {0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6,
                7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
            String[] arrayCardSuites = {
                "", "", "Ace/Clubs", "Ace/Diamonds", "Ace/Hearts", "Ace/Spades",
                "2/Clubs", "2/Diamonds", "2/Hearts", "2/Spades", "3/Clubs", "3/Diamonds", "3/Hearts", "3/Spades",
                "4/Clubs", "4/Diamonds", "4/Hearts", "4/Spades", "5/Clubs", "5/Diamonds", "5/Hearts", "5/Spades",
                "6/Clubs", "6/Diamonds", "6/Hearts", "6/Spades", "7/Clubs", "7/Diamonds", "7/Hearts", "7/Spades",
                "8/Clubs", "8/Diamonds", "8/Hearts", "8/Spades", "9/Clubs", "9/Diamonds", "9/Hearts", "9/Spades",
                "10/Clubs", "10/Diamonds", "10/Hearts", "10/Spades", "Jack/Clubs", "Jack/Diamonds", "Jack/Hearts",
                "Jack/Spades", "Queen/Clubs", "Queen/Diamonds", "Queen/Hearts", "Queen/Spades", "King/Clubs",
                "King/Diamonds", "King/Hearts", "King/Spades"
            };
            //get bet amount from user
            System.out.println("How much do you want to bet?");
            strBetAmount = dataIn.readLine();
            yourBet = Integer.parseInt(strBetAmount);
            //draw three cards (one card face up for dealer, two for player)
            //dealer first drawn card
            randomGenNum = (int) ((range * Math.random()) + 1) * 2;
            //assigning the dealer hand value & card, pulling index from array
            dealerHandValue = arrayCardValues[randomGenNum];
            //Check if the dealer was just dealt an ace
            if ((randomGenNum >= 2) && (randomGenNum <= 5)) {
                dealerHandValue = dealerHandValue + 10;
            }
            int tempDealerHandValue = dealerHandValue;
            dealerCard = arrayCardSuites[randomGenNum];
            //displaying values to the screen
            System.out.println("The dealer is showing a " + dealerCard);
            //dealers second drawn card
            randomGenNum = (int) ((range * Math.random()) + 1) * 2;
            //assigning the dealer hand value & card, pulling index from array
            dealerHandValue = arrayCardValues[randomGenNum];
            //Check if the dealer got an ace
            if ((dealerHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5)) {
                dealerHandValue = dealerHandValue + 10;
            }
            dealerCard = arrayCardSuites[randomGenNum];
            //users first drawn card
            randomGenNum = (int) ((range * Math.random()) + 1) * 2;
            //assigning the user hand value & card, pulling index from array
            userHandValue = arrayCardValues[randomGenNum];
            //Check if the user was just dealt an ace
            if ((randomGenNum >= 2) && (randomGenNum <= 5)) {
                userHandValue = userHandValue + 10;
            }
            int tempUserHandValue = userHandValue;
            userCard = arrayCardSuites[randomGenNum];
            //displaying values to the screen
            System.out.println("Your first card is " + userCard);
            //users second drawn card
            randomGenNum = (int) ((range * Math.random()) + 1) * 2;
            //assigning the user hand value & card, pulling index from array
            userHandValue = arrayCardValues[randomGenNum];
            //Check if the user got an ace
            if ((userHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5)) {
                userHandValue = userHandValue + 10;
            }
            userCard = arrayCardSuites[randomGenNum];
            //displaying values to the screen
            System.out.println("Your second card is " + userCard);
            System.out.println("Your total hand value is: " + (tempUserHandValue + userHandValue));
            //check for the either player having a blackjack(hand value = 21)
            if ((dealerHandValue == 21) && (userHandValue != 21)) {
                //dealer = 21 user != 21 dealer wins, kitty - bet, game over 
            }
            if ((dealerHandValue != 21) && (userHandValue == 21)) {
                //user = 21 dealer != user wins, kitty + bet, game over
            }
            if ((dealerHandValue == 21) && (userHandValue == 21)) {
                //dealer and user = 21 noone wins, users kitty is not changed, game over
            }
            //if neither player has 21 continue through the loop
//start the users loop
            //while(userHandValue < 21)
            if (userHandValue < 21) {
                System.out.println("Would you like to hit? (Y/N)");
                hit = dataIn.readLine();
                hit = hit.toUpperCase();
            }
            if (hit.compareTo("Y") == 0) {
                again = true;
            } else {
                again = false;
            }
            if ((again) == (true)) {
                //users next card draw
                randomGenNum = (int) ((range * Math.random()) + 1) * 2;
                //assigning the user hand value & card, pulling index from array
                tempUserHandValue = arrayCardValues[randomGenNum];
                //Check if the user was dealt an ace
                if ((tempUserHandValue != 11) && (randomGenNum >= 2) && (randomGenNum <= 5)) {
                    tempUserHandValue = userHandValue + 10;
                }
                userCard = arrayCardSuites[randomGenNum];
                //displaying values to the screen

                System.out.println("Your next card is " + userCard);
                //adding new card to total hand value and displaying to user
                userHandValue = userHandValue + tempUserHandValue;
                System.out.println("Your new total hand value is: " + (userHandValue));
            }
        } while (again == true);
4

1 回答 1

0

您在循环中声明了几个变量(包括userHandValue),这意味着在每次迭代中都会为它们创建一个新实例。

所有需要在迭代之间携带状态的变量都应该在循环之外声明,或者它们应该声明为类属性。

于 2012-11-01T13:04:46.277 回答