0

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);
}
2
  • You just save every number to the same variable, aka "a". Commented Apr 11, 2018 at 11:46
  • 1
    using a both as a variable to save the entered number as well as to controll your for loop is not a very good idea (Or plainly put: It won't work that way). Especially since that way your loop will not run 10 times but until the user enters a number >=10. Commented Apr 11, 2018 at 11:49

3 Answers 3

3

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) {
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

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);
}

3 Comments

I'm sorry for asking this silly question but why do we scan the numbers for two times? Like we already used it to read the first number, then are we using it again to store the input?
To be exact we scan the numbers 10 times. In my example I do it one time outside the loop to read the first number. Cause the first number is a special case, we use it to initialize min and max. Then at every loop we need to read a new number from user in order to compare it to min and max. An this 9 times. So at the end we've read 10 numbers and obtain the min and max of these 10 numbers.
@NorSaffia This is something you did already in your code. Since scan.nextDouble was in your for-loop, you scan 10 times. To obtain 10 numbers. You write it only one time cause you decided to manage the particular case of the first number in the loop using a if (if a == 0). I decided to manage this case outside the loop. This change nothing to the result it's just a design choice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.