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.