1

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

6 Answers 6

8

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Might be an idea: not initialize the value of smallest to Integer.MAX_VALUE. (if no int's are read, it would lead to a false result). Instead, initialize it to the first integer entered, afterwards using a loop.
@Stultuske My second option is equivalent to your suggestion. But feel free to post your own answer if you like.
@Stultuske Sorry, I didn't notice that you had already done so. But now that you've edited it, you have earned my upvote.
I had missed your second part as well. That indeed made my remark quite ... pointless :)
@DavidWallace Thanks that worked. However, I am not completely understanding what adding the i == 1 in the condition is doing. Do you mind briefly explaining?
|
5

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

6 Comments

Just to complete your answer, initializing the smallest to 0 can yield incorrect results. Better to initialize it to the largest integer value.
Which is a possibility. but, if there are no elements. why not initialize the value to the first value read, and afterwards do a loop?
That would be another way of getting around it.
IMHO, is you want to find minimum value with empty ints, it should throw an Exception. If you call Collections.min() with empty collection, it will throw a NoSuchElementException. Because if there's no element, calling min with such collection is meaningless and considered an error.
@Nier true, but in this case, we don't start with a collection or array we can test up front, before we set the default value, that would take a somewhat bigger refactoring of the code: first read the values into a collection, then go look for the smallest member.
|
2

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

Comments

2

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;

1 Comment

use smallest = Integer.MAX_VALUE. bad idea to give a number value 999999.
0

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.

Comments

0

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

Comments

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.