Java 即使应用了布尔测试,循环也不会结束
它应该退出,假设您显示的代码是准确的,并且您在某个点实际输入了aJava 即使应用了布尔测试,循环也不会结束,java,while-loop,Java,While Loop,它应该退出,假设您显示的代码是准确的,并且您在某个点实际输入了ab。也许您的实际问题是,即使您为账单输入b,它也会要求提供数量。在我看来,你可能会认为这是一个没有结束的循环 如果输入b后跟一个数量,但仍然没有退出,那么我显然错了 如果它确实在金额后退出,那么只需重新构造代码,以便在输入b时不要求数量 这可能只是一个简单的替换问题: boolean flag = true; while(flag == true){ System.out.println("Type in th
b
。也许您的实际问题是,即使您为账单输入b
,它也会要求提供数量。在我看来,你可能会认为这是一个没有结束的循环
如果输入b
后跟一个数量,但仍然没有退出,那么我显然错了
如果它确实在金额后退出,那么只需重新构造代码,以便在输入b
时不要求数量
这可能只是一个简单的替换问题:
boolean flag = true;
while(flag == true){
System.out.println("Type in the item u wish to purchase: chocolate'c',apple'a',meat'm',eggs'e',pencils'p' Bill:'b'");
sign = in.next();
ch = sign.charAt(0);
System.out.println("Enter the quantity");
amount = in.nextInt();
if(ch == 'c'){
Item_sel.add("Chocolate");
quantity.add(amount);
Stock[0]=Stock[0]-amount;
}else if(ch == 'a'){
Item_sel.add("apples");
quantity.add(amount);
Stock[1]=Stock[1]-amount;
}else if(ch == 'm'){
Item_sel.add("meat");
quantity.add(amount);
Stock[2]=Stock[2]-amount;
}else if(ch == 'e'){
Item_sel.add("eggs");
quantity.add(amount);
Stock[3]=Stock[3]-amount;
}else if(ch == 'p'){
Item_sel.add("pencils");
quantity.add(amount);
Stock[4]=Stock[4]-amount;
}
if(ch == 'b'){
String[] myArray = new String[Item_sel.size()];
Item_sel.toArray(myArray);
int[] myArray2 = new int[quantity.size()];
quantity.toArray(myArray);
change(myArray,myArray2);
flag = false;
}
}
与:
例如,以下程序(稍作修改以使其完整):
看起来b
没有退出,至少在开始时是这样。但是,当您输入金额时,它会:
import java.util.Scanner;
public class Test {
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
boolean flag = true;
while(flag == true){
System.out.println("Type in 'c', 'a', 'm', 'e', 'p' or 'b'");
String sign = in.next();
char ch = sign.charAt(0);
System.out.println("Enter the quantity");
int amount = in.nextInt();
if(ch == 'c'){
}else if(ch == 'a'){
}else if(ch == 'm'){
}else if(ch == 'e'){
}else if(ch == 'p'){
}
if(ch == 'b'){
flag = false;
}
}
System.out.println("Done.");
}
}
按建议进行修复:
Type in 'c', 'a', 'm', 'e', 'p' or 'b'
c
Enter the quantity
3
Type in 'c', 'a', 'm', 'e', 'p' or 'b'
p
Enter the quantity
2
Type in 'c', 'a', 'm', 'e', 'p' or 'b'
b
Enter the quantity
99
Done.
删除类型b
的不需要的金额输入:
import java.util.Scanner;
public class Test {
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
boolean flag = true;
while(flag == true){
System.out.println("Type in 'c', 'a', 'm', 'e', 'p' or 'b'");
String sign = in.next();
char ch = sign.charAt(0);
if (ch != 'b') {
System.out.println("Enter the quantity");
int amount = in.nextInt();
}
if(ch == 'c'){
}else if(ch == 'a'){
}else if(ch == 'm'){
}else if(ch == 'e'){
}else if(ch == 'p'){
}
if(ch == 'b'){
flag = false;
}
}
System.out.println("Done.");
}
}
顺便说一句,请不要做以下事情:
Type in 'c', 'a', 'm', 'e', 'p' or 'b'
c
Enter the quantity
3
Type in 'c', 'a', 'm', 'e', 'p' or 'b'
p
Enter the quantity
2
Type in 'c', 'a', 'm', 'e', 'p' or 'b'
b
Done.
如果您的变量命名得很好,那么只使用布尔值就更具可读性,例如:
while (flag == true) ...
while (userInsane == false) ...
那么,循环条件结束时,标志永远不会设置(并保持)为false..ya但标志开始时为true,结束时应为false wats problemch从不等于b来更改布尔值。为什么如果用户输入“b”@user3901683会发生什么问题?附加调试器并逐步完成代码。注意所发生的事情——这是预期的吗?
Type in 'c', 'a', 'm', 'e', 'p' or 'b'
c
Enter the quantity
3
Type in 'c', 'a', 'm', 'e', 'p' or 'b'
p
Enter the quantity
2
Type in 'c', 'a', 'm', 'e', 'p' or 'b'
b
Done.
while (flag == true) ...
while (userInsane == false) ...
while (moreInput) ...
while (! dataStreamClosed) ...