Search code examples
javaif-statementwhile-loopiteratoraverage

display min and max value of user input and also find the average


Hi there I am a beginner, I have looked at similar query's but i was wondering if anyone could help me with my code. I am working on an assignment to receive numbers from user input and print out the maximum value and minimum value that the user has inserted. I was wondering how I would do this without using arrays. This is what I have so far:

  public void analyseLevels() {
    UI.clearText();
    UI.printf("Insert pollution levels followed%nby a space in between%nuse 
   'done' to insert:%n");
    int sum = 0;
    int count = 0;
    int input = 0;
    double av = 0;
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
        while(UI.hasNextInt()){
        int  amt = UI.nextInt();
    // Initialise variables
    // Prompt for input
    // Loop, reading numbers and updating variables
    // Compute and print out the analysis
        if( amt > 0){

        if(amt > max){
          max = amt;

        }

        if(amt < min){
          min = amt;

        }

        if(amt >= 120){
          UI.printf("Dangerous level: %dppb%n", amt);
        }

        sum += amt;
        count++;
      }

        av = ( sum ) / ( count );
        UI.println( "Average = " + av );
        UI.println( "Max = " + max );
        UI.println( "Minimum = " + min );

   }
    UI.nextLine(); // to clear out the input
}

Solution

  • You are putting while loop end bracket } wrongly.

    put your while loop end bracket before calculating average