gpt4 book ai didi

Java:如何询问用户是否想再玩一次

转载 作者:行者123 更新时间:2023-12-01 22:51:46 25 4
gpt4 key购买 nike

如何询问用户是否想再玩一次。Ø 我必须要求用户为此输入一个字符 - ‘y’或‘n’(确保不区分大小写)

如果用户输入“y”,那么我希望它循环问题“您想玩多少轮”,然后循环“[代码块]”。

    //asks user for their name
System.out.println("\nWhat is your name?");
name = in.nextLine();

//asks user the number of rounds they want to play
System.out.println("How many rounds would you like to play?");
numRound = in.nextInt();

char cont = 'y';

do {
[block of code ]
}

} while (roundnum <= numRound);
System.out.println("Would you like to play again? (y/n)");
cont = in.next().charAt(0);
while (cont == 'y');
}

最佳答案

您使用 while 循环来检查用户是否想再次玩游戏的做法是正确的,但您在错误的位置开始循环。 while 循环应该在您想要重复的任何操作之前开始。所以伪代码大致是:

while (cont == 'y'){
//Enter game code here
System.out.println("Would you like to play again? (y/n)");
cont = in.next().charAt(0);
}

此外,永远不要以分号结束条件。它不会编译。

关于Java:如何询问用户是否想再玩一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58455400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com