Java 使用循环的初学者二十一点游戏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21394716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:26:50  来源:igfitidea点击:

Beginners blackHyman game using loops

javaloopsblackHyman

提问by user3242607

I'm trying to create a blackHyman program for school and I don't understand why my program keeps starting over after i ask for another card once I get my first two cards. Any help would be great. My code for everything is below.

我正在尝试为学校创建一个 21 点程序,但我不明白为什么我的程序在我拿到前两张牌后要求另一张牌后一直重新开始。任何帮助都会很棒。我的所有代码都在下面。

import java.util.Scanner;
import java.util.*;

public class BlackHymanGame {

    public static void main(String[] args) {

        String anotherCard, playAgain = "y", ctn;
        int nextCard, card1, card2, dCard1, dCard2;
        int cardTotal = 0, dTotal = 0;

        Scanner keyboard = new Scanner(System.in);

        Random random = new Random();

        // Begin dealing the players first two cards

        while (playAgain == "y")
        {
            //dealers first two random cards
            dCard1 = random.nextInt(10) + 1;
            dCard2 = random.nextInt(10) +1;

            //players first two random cards and card total
            card1 = random.nextInt(10) + 1;
            card2 = random.nextInt(10) + 1;
            cardTotal = card1 + card2;

            //Dealers two card total and display only one dealer card
            dTotal = dCard1 + dCard2;
            System.out.println("Dealer shows: " + dCard1);

            //Display players first two cards & card total
            System.out.println("First Cards: " + card1 + ", " +card2);
            System.out.println("Total: "+ cardTotal);

            System.out.print("Another Card (y/n)?: ");
            anotherCard = keyboard.nextLine();

            while (anotherCard == "y")
            {
                nextCard = random.nextInt(10) + 1;
                cardTotal += nextCard;
                System.out.println("Card: " + nextCard);
                System.out.println("Total: " + cardTotal);

                if (cardTotal > 21)
                {
                System.out.println("You busted, Dealer Wins");
                System.out.println("Do you want to play again? (y/n): ");
                playAgain = keyboard.nextLine();
                }   
                if (cardTotal < 21)

                System.out.print("Another Card (y/n)?: ");
                anotherCard = keyboard.nextLine();
                if (anotherCard == "n")
                System.out.print("Press c to continue dealers cards");
                ctn = keyboard.nextLine();


                while (ctn == "c" && dTotal < 17)
                {
                    nextCard = random.nextInt(10) + 1;
                    dTotal += nextCard;

                    if (dTotal > 21)
                    {
                    System.out.println("Dealer Busts, You Win!");
                    System.out.println("Play Again? (y/n): ");
                    playAgain = keyboard.nextLine();
                    if (playAgain.equalsIgnoreCase("y"))
                            playAgain = "y";
                        else
                            System.exit(0);
                    }

                }

            }

        }
    }
}

回答by Bohemian

This expression:

这个表达:

if (playAgain == "y")

will never be true, because the ==operator is only true if both operands are the sameobject. To compare the valueof Strings,use equals():

永远不会为真,因为==只有当两个操作数是同一个对象时,运算符才为真。要比较字符串的,请使用equals()

if (playAgain.equals("y"))


Don't feel bad. The problem is with the language - it was a stupid choice to use the ==operator like that. A large proportion of questions here that this issue at their root.

不要感觉不好。问题在于语言 - 使用这样的==运算符是一个愚蠢的选择。这里很大比例的问题说明了这个问题的根源。