Java 多模块条件下的While循环
主要是寻找while循环的最后一行,因为我在班上做游戏策划,我想在猜测不等于secretcode的时候继续猜测。谢谢Java 多模块条件下的While循环,java,while-loop,bluej,Java,While Loop,Bluej,主要是寻找while循环的最后一行,因为我在班上做游戏策划,我想在猜测不等于secretcode的时候继续猜测。谢谢 if (guess1 == secretcode) { System.out.print (" You have guessed one correctly in the right spot!"); } if (guess2 == secretcode1) { System.out.
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)运算符代替逗号
注意我添加的额外括号集。为了可读性,保留您已经拥有的内容是很好的
正如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)
使用
和&
(AND)运算符代替逗号
注意我添加的额外括号集。为了可读性,保留您已经拥有的内容是很好的
正如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)
使用AND运算符
&&
while (guess1 != secretcode && guess2 != secretcode1 && guess3 != secretcode2 && guess4 != secretcode3);
使用AND运算符
&&
while (guess1 != secretcode && guess2 != secretcode1 && guess3 != secretcode2 && guess4 != secretcode3);
将条件与AND(&&
)或(|
)组合:
将条件与AND(&&
)或(|
)组合:
您应该使用
&&
操作符,它允许您检查多个条件。
代码:
注意:如果使用&&则所有条件都必须为true才能执行循环。如果希望在只有一个条件为真时执行循环,请使用
|
您应该使用&&
运算符,它允许您检查多个条件。
代码:
注意:如果使用&&则所有条件都必须为true才能执行循环。如果您希望在只有一个条件为真时执行循环,请使用
|
您是否尝试查找如何进行while循环?我只是做了一个快速搜索,发现了这个你有没有试着查找如何做一个while循环?我只是快速搜索了一下,找到了这个,你也可以对if
语句使用&&
,这样你就不必复制和粘贴所有内容4次了。@Jesper,当然可以。关于OP的好建议,我会在我的答案中包括这一点。do
也不见了。你也可以对if
语句使用&
,这样你就不必复制和粘贴所有内容4次了。@Jesper,当然可以。关于OP的好建议,我会在我的答案中包括这一点。do
也不见了。
do {
//loop body
} while(guess1 != secretcode && guess2 != secretcode1 && guess3 !=secretcode2 && guess4 != secretcode3);