简体   繁体   中英

Finding min and max using Java

Please help me to figure out my mistakes. When I input scores in ascending order like 4,5 the minimum is given as 100.I don't know how to change it then?

Here is my code :

   float score=0,min=100,max=0,sum1=0,count=0,sum2=0;
    float average,sd;
    Scanner a=new Scanner(System.in);

    while(score>=0)

    {
        System.out.println("enter the score(a negative score to quit)");
        score=a.nextInt();
      if(score<0)

         break;  

         if(score>=max){   
     max=score;
     sum1+=max;  



   }

      else 
     {
     min=score;
     sum2+=min;                         

     }


      count++;
             } 

      average=(sum1+sum2)/(count++ );

    System.out.println("the average : " + average);
    System.out.println( "the maximum score: "+ max);


    System.out.println("the min score: "+ min );

change your else to else if (score < min) and you are getting the correct minimum.

The reason why, it checks if score if greater then the current max , if this is not the case then it simply does assume, due to the else, that score is the new min .

if (score >= max) {
    max = score;
}
else if (score < min){
    min = score;
} 
// Just add the score value to the sum, it doesn´t matter if it´s min or max
sum2 += score;
count++;

Simplify:

while((score = a.nextInt()) >= 0) {
    min = Math.min(min, score);
    max = Math.max(max, score);
    sum += score;
    average = sum / count++;
}

I think you're overcomplicating the problem: you should try thinking about the task at hand in logical steps:

  1. Have users input numbers
  2. Add the next int that came in to the total score
  3. Check if the next int is > last known maximum, if so, set the last known maximum to next int's value
  4. Else check if the next int < last known minimum, if so, set the last known minimum to next int's value
  5. Continue while there is more input available
  6. Print the maximum score
  7. Calculate and print the average score (totalScore / amountOfInputs)
  8. Print the minimum score

That'd look like this:

import java.util.*;

public class MaxMinAverage {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double score, currentMinimum = Integer.MAX_VALUE, currentMaximum = Integer.MIN_VALUE, count;
        while (in.hasNextInt()) {
            int nextInt = in.nextInt();
            if (nextInt < 0) { //break out of the loop
                break;
            } else {
                score += nextInt;
                count++;
                if (nextInt > currentMaximum) {
                    currentMaximum = nextInt;
                } else if (nextInt < currentMinimum) {
                    currentMinimum = nextInt;
                }
            }
        }
        System.out.println("Max: " + currentMaximum);
        System.out.println("Avg: " + (score / count));
        System.out.println("Min: " + currentMinimum);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM