[英]Using a do while loop with a user inputted String
Am currently working on a question for my Java class, I'm doing do-while loops and I'm having trouble when my while condition has to do with a user inputted String.目前我正在为我的 Java class 解决一个问题,我正在做 do-while 循环,当我的 while 条件与用户输入的字符串有关时我遇到了麻烦。 The code compiles but no matter what I enter it fails the while condition and does the loop again.
代码可以编译,但无论我输入什么,它都会使 while 条件失败并再次执行循环。 This even occurred when I hardcoded the value of unit.
这甚至发生在我硬编码 unit 的值时。
I tried looking up the solution online but every example I can find uses a user inputted int value instead of a String我尝试在线查找解决方案,但我能找到的每个示例都使用用户输入的 int 值而不是 String
public class unit {
public static void main (String[] args) {
String prompt = "Please enter your preferred unit of mass (kg, lb, g or oz): ";
System.out.println(preferredUnit(prompt));
}
public static String preferredUnit(String prompt) {
Scanner sc = new Scanner(System.in);
String unit = "";
do{
if (!unit.equals("")) {
System.out.println("Sorry but " + unit + " is not a valid unit type");
}
System.out.println(prompt);
unit = sc.nextLine();
} while(!unit.equals("kg") || !unit.equals("lb") || !unit.equals("g") || !unit.equals("oz") );
return "Unit of mass: " + unit;
}
}
!=A || !=B
is not what you're looking for.不是你要找的。 If I enter A, it's not B, so it fails.
如果我输入A,它不是B,所以它失败了。 If I enter B, it's not A, so it fails.
如果我输入B,它不是A,所以它失败了。 Instead you're looking for
!=A && !=B
相反,您正在寻找
!=A && !=B
Eg例如
while(!unit.equals("kg") || !unit.equals("lb") || !unit.equals("g") || !unit.equals("oz"))
Should be应该
while(!unit.equals("kg") && !unit.equals("lb") && !unit.equals("g") && !unit.equals("oz"))
Two possible Fixed of your problem.两种可能解决您的问题。
One Way:单程:
public static void main (String[] args) {
String prompt = "Please enter your preferred unit of mass (kg, lb, g or oz): ";
System.out.println(preferredUnit(prompt));
}
public static String preferredUnit(String prompt) {
Scanner sc = new Scanner(System.in);
String unit = "";
do{
if (!unit.equals("")) {
System.out.println("Sorry but " + unit + " is not a valid unit type");
}
System.out.println(prompt);
unit = sc.nextLine();
} while(!unit.equals("kg") && !unit.equals("lb") && !unit.equals("g") && !unit.equals("oz") );
return "Unit of mass: " + unit;
}
Second way:第二种方式:
public static void main (String[] args) {
String prompt = "Please enter your preferred unit of mass (kg, lb, g or oz): ";
System.out.println(preferredUnit(prompt));
}
public static String preferredUnit(String prompt) {
Scanner sc = new Scanner(System.in);
String unit = "";
do{
if (!unit.equals("")) {
System.out.println("Sorry but " + unit + " is not a valid unit type");
}
System.out.println(prompt);
unit = sc.nextLine();
} while(!(unit.equals("kg") || unit.equals("lb") || unit.equals("g") || unit.equals("oz")) );
return "Unit of mass: " + unit;
}
The do-while
loop says: do something while a condition is true
. do-while
循环说: do something while a condition is true
。 Your condition is:你的条件是:
!unit.equals("kg") || !unit.equals("lb") || !unit.equals("g") || !unit.equals("oz")
and it must be false
to exit from the do-while
loop.并且退出
do-while
循环必须为false
。
If you enter kg
, the first condition ( .unit.equals("kg")
) is false
and it is what you want, but unfortunately, because the next condition ( .unit.equals("lb")
) is true
and it is in OR ( ||
), you remain in the do-while
loop whatever is your input.如果你输入
kg
,第一个条件( .unit.equals("kg")
)是false
的,这是你想要的,但不幸的是,因为下一个条件( .unit.equals("lb")
)是true
,它在OR ( ||
) 中,无论您的输入是什么,您都将留在do-while
循环中。
You need to change the condition using AND ( &&
):您需要使用AND (
&&
) 更改条件:
!unit.equals("kg") && !unit.equals("lb") && !unit.equals("g") && !unit.equals("oz")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.