简体   繁体   English

Java中的while循环(二十一点)

[英]while loop in java (blackjack)

I am trying to program a basic game of Blackjack. 我正在尝试编写二十一点的基本游戏。 The problem I am having is I can not get the program to allow input after the first choice within the loop. 我遇到的问题是,在循环内进行第一选择后,我无法使程序允许输入。 For example if the player chooses to hit on their first turn, it works, but on their next turn I can not get the program to allow input. 例如,如果玩家选择在第一回合击球就可以,但是在第二回合我无法获得允许输入的程序。 here is what i have. 这就是我所拥有的。 any suggestions? 有什么建议么?

import java.util.Scanner;

public class BlackJack {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Welcome to BlackJack");
    int guess = 1+(int)(Math.random()*13);
    int guess2 = 1+(int)(Math.random()*13);
    int answer = guess + guess2;
    System.out.println("Player 1 hand: " + answer);
    System.out.println("Do you want to hit(1) or stay(2)");

    boolean looping = true;
    while(looping){
        int choice = scan.nextInt();
        int guess3 = 1+(int)(Math.random()*13);
        int hand = guess3 + answer;
    if(choice == 1){
        if(guess3 == 1){
            System.out.println("Player 1 drew an Ace!");
            System.out.println("Player 1 hand: " + hand);
            System.out.println("Do you want to hit(1) or stay(2)");
        }else if(guess3 > 1 && guess3 <= 10){
            System.out.println("Player 1 drew a"  + guess3 +"!");
            System.out.println("Player 1 hand: " + hand);
            System.out.println("Do you want to hit(1) or stay(2)");
        }else if(guess3 == 11){
            System.out.println("Player 1 drew a Jack!");
            System.out.println("Player 1 hand: " + hand);
            System.out.println("Do you want to hit(1) or stay(2)");
        }else if(guess3 == 12){
            System.out.println("Player 1 drew a Queen");
            System.out.println("Player 1 hand: " + hand);
            System.out.println("Do you want to hit(1) or stay(2)");
        }else if(guess3 == 13){
            System.out.println("Player 1 drew a King!");
            System.out.println("Player 1 hand: " + hand);
            System.out.println("Do you want to hit(1) or stay(2)");
        }
        if(choice == 2){
            looping = false;
        System.out.println("Player 1 has decided to stay");
        }if(hand > 21){
            looping = false;
            System.out.println("You have busted!");
    }

Your problem is that you are literally taking the user's input only once. 您的问题是您实际上只接受用户输入一次。

Instead of: 代替:

int choice = scan.nextInt();
while(choice == 1){
    // etc.
}

... you should implement an infinite loop: ...您应该实现一个无限循环:

while (true) {
    int choice = scan.nextInt();
}

... Also, you should catch InputMismatchException s for unexpected input types, and allow the user to get out of the loop. ...此外,您应该捕获InputMismatchException的意外输入类型,并允许用户跳出循环。

Altogether I would recommend: 我一共建议:

  • using the infinite loop as per above 使用上面的无限循环
  • using Scanner#nextLine instead of Scanner#nextInt 使用Scanner#nextLine而不是Scanner#nextInt
  • getting a String as a result 结果得到一个String
  • checking for something starting with "q" or such like for the user wanting to quit 检查是否以“ q”开头的内容或想要退出的用户之类的内容
  • otherwise, attempting to parse an Integer out of the String with Integer.parseInt and catching the eventually resulting NumberFormatException 否则,尝试使用Integer.parseIntString解析出一个Integer并捕获最终导致的NumberFormatException

Also, I'd strongly recommend using a switch statement to categorize the Integer from the user, instead of the messy if-then-else. 另外,我强烈建议您使用switch语句对用户的Integer进行分类,而不要使用杂乱的if-then-else。

This is because in every if statement you break from the loop and do not enter it again. 这是因为在每个if语句中,您都从循环中break并且不再再次输入。

What you need is a looping condition which is true until either the player busts or chooses to stay and don't break from the loop in every if statement, instead i think you might want to use continue 你需要的是一个循环条件这是真的,直到玩家萧条还是选择留下来,不break从循环中每一个if语句,而不是我想你可能想使用continue

boolean looping = true;
while (looping){
    int choice = scan.nextInt();
    //.... edit
    if (choice == 1){
         //Do your card/hand handling code here
    }
    //.... end of edit
    else if(choice == 2 || hand >= 22){
         looping = false;
         continue;  //Alternatively just break;
    }
}

Also you could reduce all your if statements to fewer cases as for 2-10 you could just do this 另外,您可以将所有if语句减少为更少的情况,例如2-10则可以

if(guess3 > 1 && guess3 <= 10){
    System.out.println("Player 1 drew an "  + guess3 +"!");
    System.out.println("Do you want to hit(1) or stay(2)");
}

EDIT In response to your comments. 编辑回应您的评论。

The program will keep adding cards as all your card handling code needs to be in the following condition 该程序将继续添加卡,因为您所有的卡处理代码都必须处于以下条件

if (choice == 1){
     //Do your card/hand handling code here
}

Currently this is not the case so your program with just add cards every time as you don't check what the user has chosen. 当前情况并非如此,因此您的程序每次都只添加卡片,因为您无需检查用户选择了什么。

Your use of the break statements means that your loop performs a very limited function, more like an if. 使用break语句意味着您的循环执行非常有限的功能,更像是if。 On breaking out of the loop you have no more code to run. 脱离循环后,您无需再运行任何代码。

If your loop worked your use of answer and hand variables means that you would never get a correct value (each time around the loop just adding the original two values to the third value). 如果循环有效,则使用answer和hand变量将意味着您永远不会获得正确的值(每次循环时,只需将原始的两个值添加到第三个值)。 Either store the cards in a list, or keep a cumulative score. 可以将卡片存储在列表中,或者保持累积分数。

You have a lot of repetition, consider using a switch case on guess3 where 1 outputs the Ace statement 2 through 10 are all the same (so, no break statements) 'System.out.println("Player 1 drew a " + guess3 + "!");'. 您的重复很多,请考虑在guess3上使用切换用例,其中1的输出Ace语句2至10都是相同的(因此,没有break语句)'System.out.println(“播放器1绘制了一个+” guess3 + “!”);'。 This can be followed by the prompt for stick/twist and the reading of the next value. 紧接着是提示粘着/扭转和读取下一个值的提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM