为什么在输入0时我的计数增加,它应该退出循环

时间:2015-09-08 05:06:14

标签: java while-loop counter minmax

我整夜都在努力,但无法做出任何改变。我希望我的代码汇总用户输入的所有数字,计算用户输入数字的次数。然后计算平均值。 然后找到最大和最小,很容易。好吧是的,如果我被允许使用数组,但这是审查,我讨厌循环。

这是我的代码。

        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而不是用户输入的内容。任何和所有的帮助非常感谢。

P.S。我尝试了很多方法,但它不起作用。如果我试图改变我的计数从-1开始它解决我手头的平均问题,但计数增加反正所以我知道它不正确。最小问题也停留在那里。

谢谢你们

5 个答案:

答案 0 :(得分:0)

当你输入0时,你需要添加if条件以避免增量。

您可以使用此代码

// 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);

答案 1 :(得分: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);

答案 2 :(得分: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;


   }

答案 3 :(得分:-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("");
      }
 }
}
}

答案 4 :(得分:-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);



    }

}