简体   繁体   中英

Find a maximum and minimum value using for loop

I am trying to take 10 integers from the user's input and find the minimum and maximum using for loop. But my final print statement just prints the list of numbers entered. I'm lost.

public static void main(String[]args) {

   Scanner scan=new Scanner(System.in);
   double a = 0;
   double max = 0;
   double min = 0;

   System.out.print("Enter ten floating points: \n");

   for(a=0; a <10; a++) {
      a=scan.nextDouble();

       if(a == 0) { 
           min=a;
           max=a; 
       } 
       else if(a < min) {
           min=a; 
       }
       else if (a > max){ 
           max=a; 
       }
   }
   System.out.println("Minimum value: " +min);
   System.out.println("Maximum value: " +max);
}

Issue is in your for loop change it to

for (int x = 0; x < 10; x++) {

there is another issue, you need to change

if(a == 0){

To

if (x == 0) {

First your code should not run correctly since you use the same variable a as the counter and as the variable to store user input. You should use two different variable.

Second declare your variable that store the input from user inside the loop, otherwise it may keep the value from the previous loop.

Third your if(a == 0) condition will reset min and max when the user enter the number 0. Which is not what you want.

Finally you should not initialize max/min like that. By defining min as 0, if the user enter only positive number the min will be 0 but the user never entered 0. You instead initialize them at the first entry from user.

This should look like this :

public static void main(String[]args) {

    Scanner scan=new Scanner(System.in);

    System.out.print("Enter ten floating points: \n");
    double tmp = scan.nextDouble(); //read first number from user

    double max = tmp; //intialize with the first input
    double min = tmp;

    for(int i=0; i <9; i++) { //from 0 to 8, 9 numbers since the first has already been read
        double a = scan.nextDouble(); //at every loop read a number from the input
        if(a < min) {
            min=a;
        }
        //removed else since max and min are independant
        if (a > max) {
            max=a;
        }
    }
    System.out.println("Minimum value: " +min);
    System.out.println("Maximum value: " +max);
}

Try this

    Scanner scan=new Scanner(System.in);
    int maximum = Integer.MIN_VALUE;
    int minimum = Integer.MAX_VALUE;
    for( int i=0; i<10 &&  scan.hasNextInt(); i++ ) {
        int next = scan.nextInt();
        maximum = Math.max( next, maximum);
        minimum = Math.min( next, minimum);
    }
    System.out.println("Found maximum :"+maximum+", minimum:"+minimum);
    scan.close();

First, we create the scanner. Next, we set a value for your maximum - since integers can be negative, we can not use 0, but have to use the smallest possible integer. Same for minimum.

In the for loop, we have to make sure that we terminate the loop after 10 iterations, or if the input stream does not have any more int's.

Next, we use the mathematical function max to find out which number is largest - the previously found maximum, or the next int from the Scanner. And same for minimum.

Finally, remeber to close the Scanner, to avoid resource leakage.

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