【问题标题】:Using while loop in java在java中使用while循环
【发布时间】:2013-08-05 00:02:06
【问题描述】:

我正在尝试读取并添加(仅)正整数,直到输入负整数。该程序应该仅在输入负整数时停止。我能想到的最好的代码是下面的代码,但问题是即使读取负整数它也不会停止。 PS:我还在学习中,请多多包涵。我尝试将 && input2!<0 添加到 while 循环条件中,但它作为一个错误出现。关于如何让它按照我想要的方式运行的任何建议?谢谢。

public class Total{
    public static void main(String[] args){

            System.out.println("Enter an integer: ");

            Scanner entry = new Scanner(System.in);
            int input1 = entry.nextInt();

            System.out.println("Enter another integer: ");
            int input2 = entry.nextInt();

            int total = input1 + input2;

            while (input2 > 0){            
                System.out.println(total + "\nEnter another interger:  ");
                total += entry.nextInt();
            }
    }
}

【问题讨论】:

    标签: java loops while-loop


    【解决方案1】:

    您需要更改正在测试的变量,此处为 input2,inside 循环。否则就没有机会退出了。您在循环中更改的只是总变量,而 input2 保持不变,因此每次测试时,它都保持 >0,这就是您卡住的原因。为什么不再试一次,这次改变 input2(并且仍然改变总数,使用 input2),看看你是否不能得到它。

    【讨论】:

      【解决方案2】:

      您的 input1 和 input2 被直接添加有缺陷的目的是在第一个输入的负数上退出,但我在下面显示的代码有效。如果输入的数字是非负数,它只会提示用户输入任何额外的数字,如果数字是非负数,它只会对输入的数字求和。

      package stackoverflow.answers;
      
      import java.util.Scanner;
      
      public class Total {
          public static void main(String[] args) {
              int total = 0, input = 0;
      
              /* Print out your "request" for an integer
               * so the user knows to enter something.
               */
              System.out.print("Enter an integer: ");
      
              /* Create a Scanner on the System.in stream */
              Scanner entry = new Scanner(System.in);
      
              /* Loop while the entered number is > 0 */
              while ((input = entry.nextInt()) > 0) {
                  /* If the input > 0, add it to total */
                  total += input;
      
                  /* Print out the request again. */
                  System.out.print("Total: " + total + "\nEnter an integer: ");
              }
      
              /* Just for kicks, print out the total
               * (only useful if the first entered number
               * is negative and the total would be 0). */
              System.out.println("Total: " + total);
          }
      }
      

      如果你想对负数求和,然后退出,我建议将 while 循环更改为 do-while 循环,如下所示:

              /* Get an integer and add it to the sum, and
               * then loop while the entered number is > 0 */
              do {
                  /* Get the integer */
                  input = entry.nextInt();
      
                  /* If the input > 0, add it to total */
                  total += input;
      
                  /* Print out the request again. */
                  System.out.print("Total: " + total + "\nEnter an integer: ");
              } while (input > 0);
      

      【讨论】:

        【解决方案3】:

        您在循环内接受的数字未存储到 input2 变量中。所以程序永远不会退出。顺便说一句,你甚至不需要 input2。变量 input1 就足够了。查看示例程序 下面:

        public static void main(String[] args){
        
        
                    System.out.println("Enter an integer: ");
        
                    Scanner entry = new Scanner(System.in);
                    int input1 = entry.nextInt();
        
        
                    int total =input1;
        
                    while (input1 > 0){
                        System.out.println(total + "\nEnter another interger:  ");
                        input1 = entry.nextInt();
                        total += input1;
        
                    }
            }
        

        【讨论】:

        • @Hovercraft Full Of Eels 我完全同意。我的错。不会给出答案。我没有看到你的答案。否则不会回答。
        【解决方案4】:
        public class WhileLoopExample {
        
            public static void main(String[] args) {
        
                int i=1;
                System.out.println("While Loop Demo");
                while(i<=10){
                    System.out.println(i);
                    i++;
                }
            }
        }
        

        【讨论】:

        【解决方案5】:
        int input1 = entry.nextInt();
        
                System.out.println("Enter another integer: ");
                int input2 = entry.nextInt();
        
                int total = input1 + input2;
        
                while (input2 > 0){            
                    System.out.println(total + "\nEnter another interger:  ");
                    total += entry.nextInt();
        

        在最后一行中,您直接获取输入而不检查其积极性,您直接将其添加到总数中。此外,在 while 循环中,您的条件是 while(input2&gt;0)。假设您的 input2 得到一个正值,那么 while 循环将永远不会结束。因此,通过将代码替换为以下代码来进行更改

        int input1 = entry.nextInt();
        
                     if(input1>0)
                    int total =input1;
        
                        while (input1 > 0){
                        System.out.println(total + "\nEnter another interger:  ");
                        input1 = entry.nextInt();
                            if(input1>0)
                        total += input1;
                       }
        

        【讨论】:

          【解决方案6】:
          while(Boolean_expression) {
             // Statements
          }
          

          Boolean_expression like true/false;

          // Statements like     System.out.prinln("Jai Shree Ram");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-03-29
            • 2021-07-18
            • 2018-09-04
            • 2018-07-07
            • 2011-01-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多