【问题标题】:While loop creates never ending input loopWhile 循环创建永无止境的输入循环
【发布时间】:2019-03-03 21:56:51
【问题描述】:

所以我希望我的程序读取输入“A”“B”或“C”并通过显示星号来显示每个输入的数量。但是我遇到了一个问题,它从不读取和显示我的输入。

我的代码如下:

if (command == 'A'){
    System.out.println("Type the additional input in a single line.");
    while(in.hasNext()){
        String input = in.next().toUpperCase();
        if(input.equals("A")){ numA++;}
        if(input.equals("B")){ numB++;}
        if(input.equals("C")){ numC++;}
    }
    System.out.println("---------------------------------------");
    System.out.printf("\n%4s      |", "A");
    for (int a = 1; a <= numA; a++) { 
        System.out.print("*");
    }
    System.out.println();
    System.out.printf("\n%4s      |", "B");
    for (int b = 1; b <= numB; b++) {
        System.out.print("*");
    }
    System.out.println();
    System.out.printf("\n%4s      |", "C");
    for (int c = 1; c <= numC; c++) { 
        System.out.print("*");}
        System.out.println();
        double gpa = ((numA*4)+(numB*3)+(numC*2)) / ((numA+numB+numC));
        System.out.println("GPA: " + gpa);
        System.out.println();
        System.out.println("---------------------------------------");
    } 

【问题讨论】:

  • in 到底是什么 - InputStreamReader?扫描仪?如果你说那是行不通的,我猜它在这里是相关的。
  • 抱歉,“in”是扫描仪的名称。
  • 能否请你把in声明和实例化。

标签: java loops input while-loop nested-loops


【解决方案1】:

意识到我错过了一个 break 语句。我的老师希望在输入任何数字后显示数据。 break 语句是我以前从未了解或使用过的东西,但我为感兴趣的人修复了它:

 if (command == 'A') {                                                               
     System.out.println("Type the additional input in a single line.");              
     while (in.hasNext()) {                                                          
         String input = in.next().toUpperCase();                                     
         if (input.equals("A")) {                                                    
             numA++;                                                                 
         }                                                                           
         if (input.equals("B")) {                                                    
             numB++;                                                                 
         }                                                                           
         if (input.equals("C")) {                                                    
             numC++;                                                                 
         }                                                                           
         if (input.compareTo("A") < 0 || input.compareTo("Z") > 0)                   
             break;                                                                  

     }                                                                               
     System.out.println("---------------------------------------");                  
     System.out.printf("\n%4s      |", "A");                                         
     for (int a = 1; a <= numA; a++) {                                               
         System.out.print("*");                                                      
     }                                                                               
     System.out.println();                                                           
     System.out.printf("\n%4s      |", "B");                                         
     for (int b = 1; b <= numB; b++) {                                               
         System.out.print("*");                                                      
     }                                                                               
     System.out.println();                                                           
     System.out.printf("\n%4s      |", "C");                                         
     for (int c = 1; c <= numC; c++) {                                               
         System.out.print("*");                                                      
     }                                                                               
     System.out.println();                                                           
     double gpa = ((numA * 4) + (numB * 3) + (numC * 2)) / ((numA + numB + numC));   
     System.out.println("GPA: " + gpa);                                              
     System.out.println();                                                           
     System.out.println("---------------------------------------");                  
 }                                                                                   

抱歉这个愚蠢的问题,我是 Java 新手。

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    相关资源
    最近更新 更多