Java 可能的选择数量有限?
这里是Java新手 有这段代码(来自教程),我想知道,我应该使用什么循环或其他方法来限制最大猜测选择,例如3? 我的意思是,用户只能猜测有限的次数,然后程序就停止了Java 可能的选择数量有限?,java,Java,这里是Java新手 有这段代码(来自教程),我想知道,我应该使用什么循环或其他方法来限制最大猜测选择,例如3? 我的意思是,用户只能猜测有限的次数,然后程序就停止了 package.com公司; 导入java.util.Scanner; 公共班机{ 公共静态void main(字符串[]args){ 分泌性内质网; 智力猜测; 布尔正确=错误; Scanner keybord=新扫描仪(System.in); 系统输出打印(“给我密码”); secretNum=keybord.nextInt()
package.com公司;
导入java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
分泌性内质网;
智力猜测;
布尔正确=错误;
Scanner keybord=新扫描仪(System.in);
系统输出打印(“给我密码”);
secretNum=keybord.nextInt();
而(!正确){
System.out.println(“猜测:”);
guess=keybord.nextInt();
如果(猜测=secretNum){
正确=正确;
System.out.println(“你是对的”);
}
else if(猜测secretNum)System.out.println(“下”);
}
}
}
您可以使用以下计数器跟踪尝试次数:
public static void main(String[] args) {
int attempts = 0;
Scanner keybord = new Scanner(System.in);
System.out.print("GIVE ME SECRET NUMBER");
int secretNum = keybord.nextInt();
while (true){
System.out.println("GUESS: ");
int guess = keybord.nextInt();
attempts++;
if (guess == secretNum){
System.out.println("YOU ARE RIGHT");
break;
}
if (attempts == 3) {
System.out.println("Max attempts!");
break;
}
else if (guess < secretNum){
System.out.println("HIGHER");
}
else if (guess > secretNum) System.out.println("LOWER");
}
}
publicstaticvoidmain(字符串[]args){
int=0;
Scanner keybord=新扫描仪(System.in);
系统输出打印(“给我密码”);
int secretNum=keybord.nextInt();
while(true){
System.out.println(“猜测:”);
int guess=keybord.nextInt();
尝试++;
如果(猜测=secretNum){
System.out.println(“你是对的”);
打破
}
如果(尝试次数==3){
System.out.println(“最大尝试次数!”);
打破
}
else if(猜测secretNum)System.out.println(“下”);
}
}
使用干净的代码结构非常容易。在循环外部声明一个变量,该变量将存储尝试次数。然后在循环条件中检查尝试次数,并在正文中增加此变量
// Add
int attempts = 0;
// Update condition
while (!correct || attempts <= 3) {
// Update variable at the end of the loop
attempts++;
}
//添加
int=0;
//更新条件
while(!correct | |尝试do while循环最适合这种情况。
它保证至少执行一次,在循环结束时而不是开始时验证循环条件,并且当您遇到这样的中断条件时,很容易中断任何循环
int guesses = 0;
final maxGuesses = 3;
final secretNum = ...
do {
System.out.println("GUESS: ");
guess = keybord.nextInt();
if (guess == secretNum){
System.out.println("YOU ARE RIGHT");
break; // no need for 'correct' value, just break out of the loop
}
else if (guess < secretNum){
System.out.println("HIGHER");
}
else if (guess > secretNum) System.out.println("LOWER");
}while (guesses++ < maxGuesses)
int猜测=0;
最终最大猜测数=3;
最终分泌数=。。。
做{
System.out.println(“猜测:”);
guess=keybord.nextInt();
如果(猜测=secretNum){
System.out.println(“你是对的”);
break;//不需要“正确”值,只需中断循环即可
}
else if(猜测secretNum)System.out.println(“下”);
}while(猜测+++
for循环也会起作用。这里,在循环开始之前检查猜测,并在循环结束时递增
for(int guesses = 0; guesses < maxGuesses; guesses++){
System.out.println("GUESS: ");
guess = keybord.nextInt();
if (guess == secretNum){
System.out.println("YOU ARE RIGHT");
break;// no need for 'correct' value, just break out of the loop
}
else if (guess < secretNum){
System.out.println("HIGHER");
}
else if (guess > secretNum) System.out.println("LOWER");
}
for(int-guesses=0;guessessecretNum)System.out.println(“下”);
}
找到答案的一种方法是用语言说出你希望发生的事情。目前,你的代码是“在不正确的时候重复”你想说“在不正确的时候重复,或者我们尝试了太多次”,这会导致你计算尝试次数,并在等待时使用“或”(如艾伦·塞雷布的答案所示),这里的大技巧是让您的思维引导您研究如何执行“或”,以及如何计算尝试次数;与其寻找代码本身,不如寻找构建代码的构造。