使用do-while循环创建Averager

时间:2017-06-29 01:30:57

标签: java loops

如果用户不这样做,该程序必须提示用户输入正数,并在用户输入-1时结束。我遇到的问题是如何在用户输入负数后计算实际平均值并且程序继续运行。它不应该计算负数。即使在用户输入负数后,如何才能打印正确的平均值?

package averager;

import java.util.Scanner;

public class Averager {

    public static void main(String[] args) {
        double sum = 0; // use for storing addition to all entered values..
        double num, count = 0;
        Scanner scan = new Scanner(System.in);

        do {

            System.out.print("Enter any positive number, enter -1 to quit: ");
            num = scan.nextDouble();
            sum = sum + num;
            count++;

            if (num <= 0) {
                System.out.println("Please enter a positive number.");

            } else if (num != -1) {
                System.out.println("Average is : " + sum + "/" + count + " = " + (sum / count));
            } else if (num == -1) {
                System.out.println("Average is : " + (sum + 1) + "/" + (count - 1) + " = " + ((sum + 1) / (count - 1)));


            }

        } while (num != -1);

    }

}

2 个答案:

答案 0 :(得分:3)

这是一个使用printf的好地方(而不是String连接)。您使用do-while的要求会使其变得复杂得多,但逻辑测试是num != -1;您可以使用continue跳过负值/零值。像,

double sum = 0, num = 0;
int count = 0;
Scanner scan = new Scanner(System.in);
do {
    System.out.print("Enter any positive number, enter -1 to quit: ");
    num = scan.nextDouble();
    if (num != -1) {
        if (num <= 0) {
            System.out.println("Please enter a positive number.");
            continue;
        }
        sum += num;
        count++;
    }
} while (num != -1);
System.out.printf("Average is : %.2f/%d = %.2f%n", sum, count, sum / count);

答案 1 :(得分:0)

逻辑中的问题,您需要在接受输入之前以及将其添加到计算之前评估输入。

这样的事情:

import java.util.Scanner;

public class Averager {

    public static void main(String[] args) {
        double sum = 0, num, count=0;
        Scanner scan = new Scanner(System.in);

        System.out.print("Enter any positive number, enter -1 to quit: ");

       do{  
          num = scan.nextDouble(); // get the input
          if(num==-1){// is it -1?          
             if(sum>0){// if yes check if there is any previous input 
               System.out.println("Average is :" + 
                                 sum + "/" + count + " = " + (sum / count));
             }
             else {
                   System.out.println("You chose to terminate the program");
              }
              break; // break the loop 
           }
           else if(num<=0){ // if it's invalid input, inform the user
                System.out.println("Please enter a positive number. Try again:");
           }
           else{// the program will reach this point if everything is ok
                // and if the user hasn't terminated the program yet
               sum +=num; // add the number 
               count++; // increment the counter
           }    
        } while(true); // this will break only when user inputs -1
        scan.close();
    }
}

<强>测试1

Enter any positive number, enter -1 to quit: -1
You chose to terminate the program

测试2

Enter any positive number, enter -1 to quit: -5
Please enter a positive number. Try again:
1
2
3
-1
Average is : 6.0/3.0 = 2.0