java中的while循环(21点)
我正在编写一个21点的基本游戏。我遇到的问题是,在循环中的第一个选择之后,我无法让程序允许输入。例如,如果玩家选择在他们的第一个回合击球,它会工作,但在他们的下一个回合,我无法让程序允许输入。这是我的。有什么建议吗java中的while循环(21点),java,loops,while-loop,Java,Loops,While Loop,我正在编写一个21点的基本游戏。我遇到的问题是,在循环中的第一个选择之后,我无法让程序允许输入。例如,如果玩家选择在他们的第一个回合击球,它会工作,但在他们的下一个回合,我无法让程序允许输入。这是我的。有什么建议吗 import java.util.Scanner; public class BlackJack { public static void main(String[] args) { Scanner scan = new Scanner(System.in); S
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!");
}
import java.util.Scanner;
公营21点{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“欢迎来到21点”);
int guess=1+(int)(Math.random()*13);
int猜测2=1+(int)(Math.random()*13);
int答案=猜测+猜测2;
System.out.println(“玩家1手:+回答”);
System.out.println(“您想点击(1)还是停留(2)”;
布尔循环=真;
while(循环){
int choice=scan.nextInt();
int guess3=1+(int)(Math.random()*13);
int hand=猜测3+回答;
如果(选项==1){
如果(猜测3==1){
System.out.println(“玩家1得了一张A!”);
System.out.println(“玩家1手:+手”);
System.out.println(“您想点击(1)还是停留(2)”;
}else if(猜测3>1&&猜测3 21){
循环=假;
System.out.println(“你已经崩溃了!”);
}
您的问题是您实际上只接受了用户的输入一次
而不是:
int choice = scan.nextInt();
while(choice == 1){
// etc.
}
…您应该实现一个无限循环:
while (true) {
int choice = scan.nextInt();
}
…此外,您应该捕获意外输入类型的inputmaschException
s,并允许用户退出循环
总之,我建议:
- 使用如上所述的无限循环
- 使用
而不是Scanner#nextLine
Scanner#nextInt
- 获取一个
字符串作为结果
- 为用户检查以“q”开头的内容或类似内容 想退出
- 否则,尝试用
并捕获最终生成的Integer.parseInt
NumberFormatException
另外,我强烈建议使用
switch
语句对用户的整数进行分类,而不是凌乱的if-then-else。这是因为在每个if语句中,您都从循环中中断,不再输入它
你需要的是一个循环条件,它是真实的,直到玩家崩溃或选择留下,并且在每个if语句中不从循环中中断
,相反,我认为你可能需要使用继续
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;
}
}
此外,您还可以将所有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)");
}
目前情况并非如此,因此您的程序每次只需添加卡片,因为您不检查用户选择的内容。使用中断语句意味着您的循环执行的功能非常有限,更像是if。一旦中断循环,您就没有更多的代码可运行
如果你的循环有效,你使用的答案和手变量意味着你永远不会得到一个正确的值(每次循环都只是将原来的两个值加在第三个值上)。要么将卡片存储在列表中,要么保留一个累积分数
你有很多重复,考虑使用Guest3上的一个切换情况,其中1个输出ACE语句2到10都是相同的(所以,没有中断语句)'St.Out.PrtLn(“玩家1画了”+ Guest3+!”)'。这之后可以提示进行粘贴/扭曲并读取下一个值。
我不确定您的程序是否破坏了您认为的方式。我看不出循环中的选项在哪里发生了更改。谢谢。您在这里说的一切都非常有用。我现在遇到的唯一问题是玩家选择的时候留港旅客(二),程序仍然会选择卡片,直到它崩溃。实际上,即使我选择点击,程序也会一直点击直到它崩溃。对于循环的每一次迭代,你需要确保每次都得到正确的输入,并且只有当他们选择点击获取卡片时。请参阅编辑,也请阅读Mena的建议。这是我修改过的代码。我添加了if(选项==1)在卡片代码之前,但我还是遇到了同样的问题。这次当我选择留下时,什么都没有输出。在原始帖子中编辑。这是因为你处理留下选择的代码现在在你的点击选择代码中,看看逻辑。如果选择是1,它永远不可能在同一代码块中是2。请参阅我对第一个代码块的编辑e在我的帖子中。我不熟悉switch语句。这是我上编程课的第一年。如果你能解释这个概念,那就太好了。我的老师并没有真正解释,而是告诉我们用谷歌搜索它。@user2741129好吧。如果你用谷歌搜索“java switch”,你会发现页面就在最上面。这对你来说解释得还不够吗?
if (choice == 1){
//Do your card/hand handling code here
}