在java中使用while循环

时间:2013-08-04 00:10:37

标签: java loops while-loop

我正在尝试读入并添加(仅)正整数,直到输入负整数。只有输入负整数时,程序才会停止。我能想出的最好的是下面的代码,但问题是即使读取负整数也不会停止。 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();
            }
    }
}

6 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:4)

你的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);

答案 2 :(得分:2)

您在循环中接受的数字不会存储到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;

            }
    }

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

while(Boolean_expression) {
   // Statements
}

Boolean_expression,例如true / false;

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