Fer*_*ian 1 java while-loop do-while
我陷入了do-while循环的困境,该循环要求使用do-while循环,直到用户输入三个正确的字符串之一
我已经试过了
Scanner input = new Scanner(System.in);
String motor = "motor";
String mobil = "mobil";
String kosong = "";
String baru = "baru";
int tahun = Calendar.getInstance().get(Calendar.YEAR);
do {
inputVehicleType();
vehicleCondition = input.next();
}
while (!(vehicleCondition.equals(motor)) || (vehicleCondition.equals(mobil)) || (vehicleCondition.equals(kosong)));
System.out.println("SUCCED");
private static void inputVehicleType() {
System.out.println(Constant.HEADER);
System.out.println("Input Jenis Kendaraan Mobil/Motor --> (jenis [motor/mobil])");
titleFooter();
}
Run Code Online (Sandbox Code Playgroud)
使用该语法,它只会检索(vehicleCondition.equals(motor)。我的预期结果是它可以检索(vehicleCondition.equals(motor),(vehicleCondition.equals(mobil),(vehicleCondition.equals(kosong))。
如果去除多余的括号可能更容易被发现,你有while (!a || b || c)代替while (!(a || b || c))。
do {
...
} while (!(vehicleCondition.equals(motor) ||
vehicleCondition.equals(mobil) ||
vehicleCondition.equals(kosong)));
Run Code Online (Sandbox Code Playgroud)
或者,等效地根据De Morgan的法律,while (!a && !b && !c):
do {
...
} while (!vehicleCondition.equals(motor) &&
!vehicleCondition.equals(mobil) &&
!vehicleCondition.equals(kosong));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
109 次 |
| 最近记录: |