java while或且同时,java do while循环在满足条件后保持循环

I am a new java programmer and I am writing a program that sets 3 model numbers for 3 printers. If user inputs wrong values I want it to continue asking user for the model number. I got it to work but only if the first value the user inters the the number for one of the 3 printers. if the first value is not one of the possible values and the second input is it still keeps repeating the loop.

package printing;

import java.util.Scanner;

public class newClass {

public static void main(String[] args) {

int count = 0;

String machine1 = "546";

String machine2 = "892";

String machine3 = "127";

Scanner s = new Scanner(System.in);

System.out.print("Model Number:");

String modelNumber = s.nextLine();

// increment count if first input value is wrong

if (!s.equals(machine1) || !s.equals(machine2) || !s.equals(machine3))

count++;

// if user inputs right value

while (true) {

if (modelNumber.equals(machine1)) {

System.out.println("Machine 1 is online");

break;

}

if (modelNumber.equals(machine2)) {

System.out.println("Machine 2 is online");

break;

}

if (modelNumber.equals(machine3)) {

System.out.println("Machine 3 is online");

break;

}

// keep looping if user does not input values for machine1, machine2 or machine3

do {

System.out.println("Try again");

System.out.print("Model Number:");

String modelNumberFalse = s.nextLine();

/* each time user gets value wrong the count variable goes up by 1 and

the loop breaks when count reaches 3 */

count++;

if (count == 3)

break;

} while (!s.equals(machine1) || (!s.equals(machine2)) || (!s.equals(machine3)) && (count < 2));

}

}

}

Also each time the user inputs the wrong value I want the count variable to increment until it reaches 3 and the do while loop breaks but it keeps asking for the model number after I've entered the wrong values more than 3 times.

解决方案

There are several problems. This line is wrong:

while(!s.equals(machine1) || (!s.equals(machine2)) || (!s.equals(machine3)) && (count < 2));

s is a Scanner, not a String, this isn't a valid comparison. Substituting modelNumber for s gives:

while(!modelNumber.equals(machine1) || (!modelNumber.equals(machine2)) || (!modelNumber.equals(machine3)) && (count < 2));

This can't be false unless modelNumber, machine1, machine2, and machine3 are all the same value.

Also testing count is messing this up and is redundant since you're testing it and breaking within the loop.

It should be

while(!modelNumber.equals(machine1)

&& (!modelNumber.equals(machine2))

&& (!modelNumber.equals(machine3)));

See DeMorgan's Laws. Applying this rule gives

while(!(modelNumber.equals(machine1)

|| modelNumber.equals(machine2)

|| modelNumber.equals(machine3)))

which may be easier to read.

Also, if you substitute "return" for "break;" along with making the change to the do-while condition, it works. So there is something else going on. Calling break in the inner do-while causes control to return to the top of the outer while loop. Adding a boolean flag that is set before you break and which is tested in the outer while loop would be one way to solve this. Or just use return.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值