0

我整晚都在做这件事,但什么也没弄明白。我希望我的代码将用户输入的所有数字相加,计算用户输入数字的次数。然后计算平均值。然后找到最大值和最小值,很容易对。好吧,是的,如果我被允许使用数组,但这是为了审查,我讨厌 while 循环。

这是我的代码。

        double integer = 1;
      //double num = 0;
        double sum = 0.0;
        double average = 0.0;
        Scanner input = new Scanner(System.in);
        int count = 0;
       // double char1=0;
        double min = integer;
        double max = integer;
      // char letter = 'q';

       while (integer != 0) {

            System.out.println("Please enter an integer: ");
            integer = input.nextInt();

            sum += integer; 
           count++;
            System.out.println("The sum of your numbers is: " + sum);
            System.out.println("The number of values entered is: " +    count);
            System.out.println("");

            if (integer > max)
                max = integer;
            else if (integer < min)
                min = integer;


       }

这是输出:

请输入一个整数:3 您的数字的总和是:3.0 输入的值的数量是:1

请输入一个整数:2 您的数字的总和是:5.0 输入的值的数量是:2

请输入一个整数:1 您的数字的总和是:6.0 输入的值的数量是:3

请输入一个整数:0 您的数字的总和是:6.0 输入的值的数量是:4

总和的平均值为:1.5 最大整数为:3.0 最小整数为:0.0

当计数增加 1 时,我的平均值会出错。但是为什么 0 被计为计数的一部分,为什么我的 min 总是输出 0 而不是用户输入的内容。非常感谢任何和所有帮助。

ps 我尝试了很多方法,但它不起作用。如果我尝试将计数更改为从 -1 开始,它可以解决我手头的平均问题,但计数无论如何都会增加,所以我知道它不正确。最小问题也仍然存在。

多谢你们

4

5 回答 5

0

尝试这个:

对于这些情况,最好使用有条件的do while. 并初始化min为允许的最大值。

  double integer;
  double sum = 0.0;
  Scanner input = new Scanner(System.in);
  int count = 0;
  double min = Double.MAX_VALUE;
  double max = 0;      
  do {
      System.out.print("Please enter an integer: ");
      integer = input.nextInt();
      if (integer >0) {
         sum += integer; 
         count++;
         System.out.println("The sum of your numbers is: " + sum);
         System.out.println("The number of values entered is: " +    count);
         System.out.println("");
         if (integer > max)
             max = integer;
         if (integer < min)
             min = integer;
      }
   } while (integer != 0);
   System.out.println("avg: "+sum/count); 
   System.out.println("max: "+max);
   System.out.println("min: "+min);
于 2015-09-08T05:23:50.843 回答
0

您将需要添加一个额外的 if 条件才能使其工作。我在您的代码中进行了如下的一些更改,并且它按预期工作。

   double integer = 1;
  //double num = 0;
    double sum = 0.0;
    double average = 0.0;
    Scanner input = new Scanner(System.in);
    int count = 0;

    double min = integer;
    double max = integer;


   while (true) {

        System.out.println("Please enter an integer: ");
        integer = input.nextInt();
         if(integer != 0)
      {
        sum += integer; 
       count++;
        System.out.println("The sum of your numbers is: " + sum);
        System.out.println("The number of values entered is: " +    count);
        System.out.println("");

        if (integer > max)
            max = integer;
        else if (integer < min)
            min = integer;
       }
       else 
           break;


   }
于 2015-09-08T05:52:08.010 回答
0

您需要添加 if 条件以避免输入 0 时增加。

您可以使用此代码

// setting starting min and max value.
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;

while (integer != 0) {

    System.out.println("Please enter an integer: ");
    integer = input.nextInt();

    sum += integer; 
    if (integer != 0) { // added if condition
        count++;
        System.out.println("The sum of your numbers is: " + sum);
        System.out.println("The number of values entered is: " +    count);
        System.out.println("");

        if (integer > max)
            max = integer;

        if (integer < min) // changed 'else if' to 'if'
            min = integer;
    }
}

System.out.println("Max : " + max);
System.out.println("Min : " + min);
于 2015-09-08T05:13:39.717 回答
-1

你可以试试下面的代码

import java.util.Scanner;

public class ComputeDemo { 
public static void main(String[] args) {
double integer = 1;
//double num = 0;
  double sum = 0.0;
  double average = 0.0;
  Scanner input = new Scanner(System.in);
  int count = 0;
 // double char1=0;
  double min = integer;
  double max = integer;
// char letter = 'q';

 while (integer != 0) {

      System.out.println("Please enter an integer: ");
      integer=input.nextInt();

      if(integer>0)
      {
      sum += integer; 
     count++;
      System.out.println("The sum of your numbers is: " + sum);
      System.out.println("The number of values entered is: " +    count);
      System.out.println("");

      if (integer > max)
          max = integer;
      else if (integer < min)
          min = integer;
      }
      else
      {
          min=0;
          System.out.println("The sum of your numbers is: " + sum);
          System.out.println("The number of values entered is: " +    count);
          System.out.println("");
      }
 }
}
}
于 2015-09-08T05:26:53.107 回答
-1
  double integer = 1;
        double sum = 0.0;
        double average = 0.0;
        Scanner input = new Scanner(System.in);
        int count = 0;
        double min = Double.MAX_VALUE;
        double max = Double.MIN_VALUE;


       while (integer != 0) {

            System.out.println("Please enter an integer(press zero to exit): ");
            integer = input.nextInt();

            if (integer > 0){
            sum += integer; 
            count++;



            if (integer > max)
                max = integer;
            if (integer < min)
                min = integer;


       }

       }


        System.out.println("The sum of your numbers is: " + sum);
        System.out.println("Your count number is: " + count);
        average = sum / count;
        System.out.println("The average of your sum is: " + average);
        System.out.println("The max integer is: " + max);
        System.out.println("The min integer is: " + min);



    }

}
于 2015-09-08T05:59:03.383 回答