1

I am working on a project for my college, so far code is working fine, except when a program tries to calculate the average all input, it always gives the wrong answer. Ex: Test input: 3 -4 5 12 -7 0 (to exit the loop)

Result -> sum: 9 Counter: 5 Average = 1.0 ? It should be 1.8

If anyone could help, please give me some advice.

public static void main(String[] args) 
{
    Scanner sc = new Scanner(System.in);
    int counter = 0;

    int intInput;
    int intLargest;
    int intSmallest;
    int intEven = 0;
    int intOdd = 0;
    int intSum = 0;
    String strMessage = "Enter a series of value (0 to quit)";    


    System.out.print(strMessage);
    System.out.println();        

   System.out.print("Enter Integer value?" + "\n");
   intInput = sc.nextInt();

        intLargest = intInput;
        if (intLargest == 0)
        {
            intLargest = 0;
        }
        else
        {
            intLargest = intInput;
        }
        /////////////////////////////////////////////////////////////

        intSmallest = intInput;
        if (intSmallest == 0)
        {
            intSmallest = 0;
        }
        else
        {
            intSmallest = intInput;
        }            



    while(intInput != 0)
    { 
    {            
      if (intInput > intLargest)
      {
          intLargest = intInput;
      }//Get the largest value        
      else if (intInput < intSmallest)
      {
          intSmallest = intInput;
      }//Get the smallest value


      if ((intInput%2) == 0)
      {
          intEven++;
      }//Get number of Even value
      else if ((intInput%2) != 0)
      {
          intOdd++;
      }//Get number of Odd value
    }


      intSum = intSum + intInput;        
      intInput = sc.nextInt();
      counter++;
    }

/********************************************/

    double doubleAvg = 0;

    if (counter > 0)
    {
        doubleAvg = intSum / counter;
    }     

/***************************************************/

    System.out.println();
    System.out.print("Smallest = " + intSmallest +"\n");        
    System.out.print("Largest = " + intLargest + "\n");
    System.out.print("Total Entered = " + counter + "\n");        
    System.out.print("Even Number = " + intEven + "\n");
    System.out.print("Odd Number = " + intOdd + "\n");
    System.out.print("Average = " + doubleAvg + "\n");
    System.out.print("SUM: " + intSum + "\n");
}

}

Duke
  • 23
  • 5

1 Answers1

-1

Both intSum and counter are declared as int. And in case of dividing int by int the result will also be int. So result of division is 1 and then it is converted to double and you get 1.0. To get correct result declare intSum as double.

Ivan
  • 7,770
  • 2
  • 17
  • 25
  • *declare `intSum` as double*... not the greatest advice... – shmosel May 31 '18 at 02:13
  • Maybe variable should be renamed, but in my opinion declaring it as double is better then casting it to double right before division or multiply by 1.0 to convert intermediate result to double @shmosel – Ivan May 31 '18 at 02:20
  • Thank you, got the issue solved, I re-declared intSum as double. – Duke May 31 '18 at 02:33