我是一名新的java程序员,我正在编写一个程序,为3台打印机设置3个型号。如果用户输入了错误的值,我希望它继续向用户询问型号。我得到了它的工作,但只有当用户的第一个值在3个打印机之一的数字之间。如果第一个值不是可能的值之一而第二个输入是,它仍然会重复循环。
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));
}
}
}
每次用户输入错误的值时,我都希望count变量递增,直到达到3并且do while循环中断,但是在我输入错误的值超过3次之后它仍然要求输入型号。
答案 0 :(得分:6)
有几个问题。这条线错了:
while(!s.equals(machine1) || (!s.equals(machine2)) || (!s.equals(machine3)) && (count < 2));
s
是扫描程序,而不是字符串,这不是有效的比较。将modelNumber
替换为s
会产生:
while(!modelNumber.equals(machine1) || (!modelNumber.equals(machine2)) || (!modelNumber.equals(machine3)) && (count < 2));
除非modelNumber,machine1,machine2和machine3都是相同的值,否则不能为false。
同样测试计数会搞砸这个并且是多余的,因为你正在测试它并在循环中打破。
应该是
while(!modelNumber.equals(machine1)
&& (!modelNumber.equals(machine2))
&& (!modelNumber.equals(machine3)));
见DeMorgan's Laws。应用此规则提供
while(!(modelNumber.equals(machine1)
|| modelNumber.equals(machine2)
|| modelNumber.equals(machine3)))
可能更容易阅读。
另外,如果用“return”代替“break”,随着对do-while条件的改变,它起作用。所以还有其他事情要发生。在内部执行中调用break会导致控制返回到外部while循环的顶部。添加在中断之前设置的布尔标志以及在外部while循环中测试的布尔标志将是解决此问题的一种方法。或者只是使用return。
答案 1 :(得分:0)
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);
while (true)
{
System.out.print("Model Number:");
String modelNumber = s.nextLine();
// increment count if first input value is wrong
if ((!modelNumber.equals(machine1)) || (!modelNumber.equals(machine2)) || (!modelNumber.equals(machine3)))
count++;
if (count == 3)
{
System.out.println("You have utilized your maximum number of try's");
break;
}
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;
}
System.out.println("Try again");
}
}
}
希望这能解决你的问题
答案 2 :(得分:0)
在您的代码中,您最终会重复进行相同的测试。这意味着,在添加计算机时,您必须在多个位置更新代码。
软件的第一条规则是不要重复自己。当下一个人被要求有条件时,他/她将找到第一个代码块并对其进行编辑,并且可能永远不会注意到重复的阻塞。复制粘贴的代码是根或许多未来的错误。
您可以将代码简化为每次只检查一次:
import java.util.Scanner;
public class newClass {
public static void main(String[] args) {
int count = 0;
// for extra credit, try to make this an ArrayList
// so you can keep adding models as needed
// then you would adjust your tests to leverage the ArrayList
// search functions
String machine1 = "546";
String machine2 = "892";
String machine3 = "127";
Scanner s = new Scanner(System.in);
// when using a while loop, it is good practice to use a boolean
// as your logic expands, multiple tests in the loop may set the
// boolean to true or false
// it is cumbersom to have large blocks of code in your while check
boolean keepOnTrucking = true;
System.out.print("Enter Model Number:");
while (keepOnTrucking) {
String modelNumber = s.nextLine();
// when using multiple tests, it is good
// to give each test its own line and end the line
// with the logical operator that joins it to the next
// it makes it easier to read
// and easier to edit (add or remove tests)
// Logical operator note:
// Your test was: not A OR not B OR not C
// This would NEVER work, as A != B != C
// If a user entered machine2, the check would
// fail for !(machine1), OR !(machine2) OR !(machine3)
// because while (!s.equals(machine2)) would say false
// (!s.equals(machine1)) would say true, and the OR
// chain would stop and count it as an error.
// Instead you want:
// !(machine1) && !(machine2) && !(machine3)
// Thus to to error, it has to not any of the machines.
// If it is true for all three nots, then you have an error
if (!machine1.equals(modelNumber) &&
!machine2.equals(modelNumber) &&
!machine3.equals(modelNumber)) {
// you only increment on failure
count++;
// nice developers give meaningful feed back to users
if (count>=3) {
System.out.print("Out of guesses! Go Away!"); // even when it is mean
// since you are nested in one while loop,
// this will break you out
break;
} else {
System.out.print("Not a valid model number, please re-enter:");
}
} else {
// the found a machine, so exit the while loop
keepOnTrucking = false;
if (machine1.equals(modelNumber)) {
System.out.println("Machine 1 is online");
} else if (machine1.equals(modelNumber)) {
System.out.println("Machine 2 is online");
} else { // since this ins the only one left, you don't need an if clause
System.out.println("Machine 3 is online");
}
}
}
}
}