【发布时间】:2015-12-21 04:38:51
【问题描述】:
因此,当您使用 Java 编码并执行 do-while 循环时,当有多个条件要中断循环时,您必须使用 && (and) 而不是 || (要么)。我知道 && 会打破循环。
在本例中,输入 1、2、3 或 4 会中断循环,但为什么是 && !??!我的教授无法解释..
import java.util.*;
public class ErrorTrap
{
public static void main(String[] args)
{
System.out.println("Program by --");
System.out.println();
System.out.println("Error Traps");
System.out.println();
//initialize variables
Scanner input = new Scanner(System.in);
char year;
//user input
do {
System.out.print("Please input a 1, 2, 3 or 4: ");
year = input.nextLine().charAt(0);
//output
System.out.println(num);
System.out.println();
}
while(year != '1' && year != '2' && year != '3' && year != '4');
}//end main
}//end ErrorTrap
【问题讨论】:
-
&&意味着所有条件都必须为真才能继续执行,||意味着只有一个条件就足以让执行继续。 -
如果你的教授不能解释这个,也许你应该考虑换教授...
标签: java logic do-while explain