首页 文章

用户输入是的继续程序

提问于
浏览
1

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

这是我的代码

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

    做这个

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

相关问题