有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java While循环即使应用了布尔测试也没有结束

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;
        }
    }

共 (1) 个答案

  1. # 1 楼答案

    它应该退出,假设您显示的代码是准确的,并且您实际上在某个点输入了b。也许您的实际问题是,即使您为账单输入b,它也需要数量。在我看来,你可能会认为这是一个没有结束的循环

    如果输入b后跟一个数量,但仍然没有退出,那么我显然错了

    如果它确实在数量之后退出,那么只需重新构造代码,以便在输入b时不要求数量

    这可能只是一个简单的替换问题:

    System.out.println("Enter the quantity");
    amount = in.nextInt();
    

    与:

    if (ch != 'b') {
        System.out.println("Enter the quantity");
        amount = in.nextInt();
    }
    

    例如,以下程序(稍作修改以使其完整):

    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.");
       }
    } 
    

    看起来b没有退出,至少最初是这样。但是,当您输入金额时,它会:

    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.
    

    按建议进行修复:

    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.");
       }
    } 
    

    删除类型b的不需要的金额输入:

    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) ...