用户输入是的继续程序

时间:2015-03-03 22:02:49

标签: java while-loop repeat

好的,所以我的程序工作正常,但最后它询问用户是否想要输入另一组整数,如果他们输入“是”,则该过程重新开始。我的程序重新开始,但它保留了所有相同的用户输入,因此它只是不断重复原始输入的相同输出。

这是我的代码

import java.util.Scanner;

public class PositiveNegative {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Input a list of integers (end with 0): ");
        int nums = input.nextInt();

        String answer;
        int pos = 0;
        int neg = 0;
        int total = 0;
        int count = 0;
        double avg = 0;

        do {
            while (nums != 0) {
                if (nums >= 1) {
                    pos++;
                } else {
                   neg++;
                }
                total = total + nums;
                count++;

                avg = total * 1.0 / count;

                System.out.print("Input a list of integers (End with 0): ");
                nums = input.nextInt();
            }
            System.out.print("# of positives are: " + pos + "\n" + "# of negatives are : " + neg + "\n" + "The total: " + total + "\n" + "The average: " + avg);
            System.out.print("\nWould you like to continue with new inputs? ");
            answer = input.next();
        } while (answer.equalsIgnoreCase("Yes"));
    }  
}

我目前的输出如下:

  

输入整数列表(以0结尾):1

     

输入整数列表(以0结尾):2

     

输入整数列表(以0结尾): - 1

     

输入整数列表(以0结尾):3

     

输入整数列表(以0结尾):0

     

阳性数量为:3

     

否定数量为:1

     

总数:5

     

平均值:1.25

     

您想继续接受新的投入吗?是

     

阳性数量为:3

     

否定数量为:1

     

总数:5

     

平均值:1.25

     

您想继续接受新的投入吗?

它应该看起来像这样:

  

正输入数量:3

     

负输入数量:1

     

总数:5.0

     

平均值:1.25

     

您想继续接受新的投入吗?是

     

输入整数列表(以0结尾):0

     

除0

外没有输入任何数字      

您想继续接受新的投入吗?无

2 个答案:

答案 0 :(得分:0)

变量的声明应该在do ... while块内,以便在用户想要继续时可以初始化它们。

替换

 int pos = 0;
 int neg = 0;
 int total = 0;
 int count = 0;
 double avg = 0;

 do{
     //...

do {
    int pos = 0;
    int neg = 0;
    int total = 0;
    int count = 0;
    double avg = 0;
    //...

答案 1 :(得分:0)

这样做

import java.util.Scanner;

public class PositiveNegative {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String answer;
        do {
            System.out.print("Input a list of integers (end with 0): ");
            int nums = input.nextInt();

            int pos = 0;
            int neg = 0;
            int total = 0;
            int count = 0;
            double avg = 0;

            while (nums != 0) {
                if (nums >= 1) {
                    pos++;
                } else {
                   neg++;
                }
                total = total + nums;
                count++;

                avg = total * 1.0 / count;

                System.out.print("Input a list of integers (End with 0): ");
                nums = input.nextInt();
            }

            if(count == 0) {
                System.out.print("No numbers were entered except 0");
            } else {
                System.out.print("# of positives are: " + pos + "\n" + "# of negatives are : " + neg + "\n" + "The total: " + total + "\n" + "The average: " + avg);
            }
            System.out.print("\nWould you like to continue with new inputs? ");
            answer = input.next();
        } while (answer.equalsIgnoreCase("Yes"));
    }  
}