有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用dowhile循环创建一个Averager

该程序必须提示用户输入一个正数,如果不输入,则只在用户输入-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) 个答案

  1. # 1 楼答案

    这是一个使用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);
    
  2. # 2 楼答案

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

    比如:

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

    Test1

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

    Test2

    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