简体   繁体   中英

find minimum value using a for loop

I am trying to take 10 integers from the user's input and find the minimum value using a for loop.

I am struggling to correctly write the if statement. It is supposed to take the first input and make that the smallest and compare the next inputs to that.

My final print statement just prints the last number entered.

Scanner scan = new Scanner(System.in);

int smallest = 0;
int number = 0;

for (int i = 1; i <= 10; i++) {
   System.out.print("Enter a number > ");
   number = scan.nextInt();

   if (number < smallest) {
       smallest = number; 
   } else {  
       smallest = number; 
   }       
}
System.out.println("The minimum is " + smallest);

One of your problems is that you're starting with smallest = 0 , which means it will only change if one of the inputs is less than zero. There are two ways you could fix this. EITHER

  • Start with int smallest = Integer.MAX_VALUE;

OR

  • Change the condition for updating smallest to if (number < smallest || i == 1 )

Additionally, you don't want to update smallest if the if clause doesn't fire, so remove the else block.

With this:

if (number < smallest) {
    smallest = number; 
}  else {  
    smallest = number; 
}

You always override the value of smallest, whether number is smaller or not.

Remove the else block completely, and it will work.

EDIT Also: don't use 0 as default value. Take the first value you read as the 'original smallest'

System.out.print("Enter a number > ");
int smallest = scan.nextInt();
  int number = 0;

  for (int i = 1; i <= 9; i++) {
     System.out.print("Enter a number > ");
     number = scan.nextInt();
        if (number < smallest) {
           smallest = number; 
        }
  }

Try something like this

Scanner scan = new Scanner(System.in);

int smallest = 0;
int number = 0;

for (int i = 1; i <= 10; i++) {
   System.out.print("Enter a number > ");
   number = scan.nextInt();
   if (i == 1){
       smallest = number;
   }
   if (number < smallest) {
       smallest = number; 
   }

}

System.out.println("The minimum is " + smallest);

Two issues.

1 - your if should look like this (remove the else block):

if (number < smallest) {
 smallest = number; 
}

2 - you should initialize smallest to a very large number so the first number seen is always smaller than it:

int smallest = Integer.MAX_VALUE;

Solution: remove your else statement.

if (number < smallest) {
    smallest = number; 
}

Without any else . With the usage of else you set the value of smallest every time to the value entered.

This would be my preference for making the first assignment to smallest variable. Make a separate assignment to smallest altogether before the loop begins. This way we know exactly which is the first statement to assign to smallest , and as others have stated previously get rid of the else block for if statement within the for loop .

The else block is causing what OP stated as problem of prints the last number entered . N Now since the prompt is presented in 2 different places also added a String variable for 'prompt' so it can be reused. Notice for loop count reduced to 9 from 10 to keep with only prompting user input 10 times.

    Scanner scan = new Scanner(System.in);

      int smallest = 0;
      int number = 0;
      String prompt = "Enter a number > ";

      // First user prompt
      // and first assignment to 'smallest'
      System.out.print(prompt);
      smallest = scan.nextInt();

      for (int i = 1; i <= 9; i++) {
         System.out.print(prompt);
         number = scan.nextInt();

            if (number < smallest) {
               smallest = number; 
            }

      }
         System.out.println("The minimum is " + smallest);

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