0

I been working on this all night but couldn't make anything out it. I want my code to sum all the numbers the user enter, count how many times the user enters the number. then calculate the average. and then find the max and min, easy right. well yeah if i was to be allowed to use arrays but this is for review and I hate while loops.

here's my code.

        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;


       }

Here's the output:

Please enter an integer: 3 The sum of your numbers is: 3.0 The number of values entered is: 1

Please enter an integer: 2 The sum of your numbers is: 5.0 The number of values entered is: 2

Please enter an integer: 1 The sum of your numbers is: 6.0 The number of values entered is: 3

Please enter an integer: 0 The sum of your numbers is: 6.0 The number of values entered is: 4

The average of your sum is: 1.5 The max integer is: 3.0 The min integer is: 0.0

when the count increases by 1 my average comes out wrong. but why is 0 been counted as part of count and why my min always output 0 and not what the user enters. any and all help is much appreciated.

p.s. i have tried numerous ways but it doesnt work. if i try to change my count to start at -1 it solves my problem at hand with average but the count increases anyways so i know its incorrect. also the min problem stays there.

thanks guys

JavaCoder
  • 33
  • 1
  • 8
  • *"why is my count increasing when entering 0"* - Because you increment `count` BEFORE the loop exit condition is checked – MadProgrammer Sep 08 '15 at 05:07
  • 1
    add `if(integer >0) count++;` – Rustam Sep 08 '15 at 05:08
  • The average looks _right_ to me... 6 divided by 4 inputs is 1.5. Where is the problem? – Tim Biegeleisen Sep 08 '15 at 05:09
  • the problem with average is that when the counter add an extra count the average divide by 4 when the user enters only 3 numbers and the 4th is to exit the loop – JavaCoder Sep 08 '15 at 05:12
  • Thanks so much @Rustam that solved my count problem. any idea what i can do to fix the min problem? – JavaCoder Sep 08 '15 at 05:16
  • @Rustam i tried initializing double max = integer.MIN_VALUE; and vice versa but it wouldn't work. any ideas. I understand what you said with 999999999 but i want it to be rather simple than typing numbers – JavaCoder Sep 08 '15 at 05:34
  • 1
    for `min=999999` the problem is your `min is 1` so when you input min which is greater then 1 won't get assigned in `min` variable. so your should initialize your `min` with maximum value. – Rustam Sep 08 '15 at 05:35
  • @JavaCoder, you can refer my answer below. – Naman Gala Sep 08 '15 at 05:37
  • @Rustam Thanks buddy. I got it all correct. I'll post the correct code in a min. – JavaCoder Sep 08 '15 at 05:43

5 Answers5

0

You need to add if condition to avoid increment when you enter 0.

You can use this code

// 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);
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • @javaCoder change `else if (integer < min)` to `if (integer < min)`. – Naman Gala Sep 08 '15 at 05:50
  • @JavaCoder I have made some changes so that it answer your question. It is not copying. It is just to help you. And also if I am not wrong my answer was posted before you posted your solution to your own question. – Naman Gala Sep 09 '15 at 06:20
0

Try this:

For these cases it is best to use the conditional do while. And initialize min at the maximum value allowed.

  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);
SkyMaster
  • 1,323
  • 1
  • 8
  • 15
0

you will need to add an extra if condition to make it work. I have made few changes as below in your code and it is working as expected.

   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;


   }
-1

You may try the below code

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("");
      }
 }
}
}
Droy
  • 173
  • 2
  • 17
-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);



    }

}
JavaCoder
  • 33
  • 1
  • 8
  • You should add an explanation of your code instead of it just being a code dump. If this is truly an answer it should come with explanation. – takendarkk Sep 08 '15 at 11:52
  • Thanks, I tried but it wouldn't allow me. kept saying my formatting is incorrect. so i just posted my code – JavaCoder Sep 09 '15 at 06:19
  • It will allow you. Don't let an online text box defeat you. Also, don't post code that is horribly indented and full of unneeded whitespace. – takendarkk Sep 09 '15 at 08:59