gpt4 book ai didi

java - 多个条件的 while 循环

转载 作者:行者123 更新时间:2023-12-01 16:57:39 24 4
gpt4 key购买 nike

主要是寻找 while 循环所在的最后一行,因为我在类制作游戏 Mastermind,并且我想在猜测不等于密码时继续猜测。谢谢

        if (guess1 == secretcode) {
System.out.print (" You have guessed one correctly in the right spot!");
}
if (guess2 == secretcode1) {
System.out.print (" You have guessed one correctly in the right spot!");
}
if (guess3 == secretcode2) {
System.out.print (" You have guessed one correctly in the right spot!");
}
if (guess4 == secretcode3) {
System.out.print (" You have guessed one correctly in the right spot!");
}
do
{
System.out.println ("Keep guessing you have not won yet!");
System.out.println ("What is the number one peg in my code???");
guess1 = Guess.nextInt();
System.out.println ("What is the number two peg in my code???");
guess2 = Guess.nextInt();
System.out.println ("What is the number three peg in my code???");
guess3 = Guess.nextInt();
System.out.println ("What is the number four peg in my code???");
guess4 = Guess.nextInt();
}
while (guess1 != secretcode), (guess2 != secretcode1), (guess3 != secretcode2), (guess4 != secretcode3);

最佳答案

使用 && (AND) 运算符代替逗号

do{
//code to iterate here
}
while ( (guess1 != secretcode) && (guess2 != secretcode1)
&& (guess3 != secretcode2) && (guess4 != secretcode3) );

记下我添加的额外括号。为了可读性,最好保留已有的内容。

<小时/>

正如 Jesper 在下面评论的那样,您可以对 if 语句执行相同的操作。清理大量空间。

if ( (guess1 == secretcode) && (guess2 == secretcode1) 
&& (guess3 == secretcode2) && (guess4 == secretcode3) ) {

System.out.print (" You have guessed one correctly in the right spot!");
}
do {
//code to iterate here
} while (condition)

关于java - 多个条件的 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30484366/

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