0

Purpose of this code: Find the lowest integer inserted by a user.

Problem facing: When the variable thirdInt is supposed to be the lowest number, the console doesn't print out the result.

Can anybody tell me what is wrong with that part of my code?

import java.util.Scanner;

public class FindMinimum {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the first integer:");
        int firstInt = input.nextInt();
        System.out.println("Enter the second integer:");
        int secondInt = input.nextInt();
        System.out.println("Enter the third integer:");
        int thirdInt = input.nextInt();

        if(firstInt<secondInt || firstInt == secondInt) {
            if(firstInt<thirdInt || firstInt == thirdInt) {
                System.out.println("The minimum is " + firstInt);    
            }  
        }
        else if(secondInt<firstInt || secondInt == firstInt) {
            if(secondInt<thirdInt || secondInt == thirdInt) {
                System.out.println("The minimum is " + secondInt);    
            }
        }
        else if(thirdInt<firstInt || thirdInt == firstInt) {
            if(thirdInt<secondInt || thirdInt == secondInt) {
                System.out.println("The minimum is " + thirdInt);    
            }
        }
    }
}
4
  • 1
    Please indent your code properly to make it readable. Commented Jun 28, 2020 at 0:43
  • Consider using a loop to do this and a single variable to hold the minimum value. Commented Jun 28, 2020 at 0:50
  • Let's consider stepping though this program with the values 5,10,3. Is the first value less than the second (is 5<10)? Yes. Is the first value less than the third (is 5<3)? No. The program ends here. It is true that at this point we would know that if first<second and third<first, that the third value is the smallest, but there is no print statement for that. The else ifs would not run because the first if statement was true. Commented Jun 28, 2020 at 1:45
  • Also, you should really close that scanner so that you don't have a memory leak. Commented Jun 28, 2020 at 1:48

2 Answers 2

3

The answer can be very simple like the following (Like what Matthew0898 had said in the comment)

int answer = firstInt;

if secondInt < answer {
   answer = secondInt;
}

if thirdInt < answer {
   answer = thirdInt;
}

System.out.println("The minimum is " + answer); 
Sign up to request clarification or add additional context in comments.

2 Comments

I accidentally hit delete on the comment in question. I attempted to recreate the post.
@Matthew0898 said that it can be done as if you are following the variables from the first to the third with comparison to get select the lowest one. Just like a walk down. :)
1

Check what you have done with you first two outer if statements:

  • The first one: if(firstInt<secondInt ...)
  • The second one: else if((secondInt<firstInt ...)

The only way for anything to be passed down to the third if statement is if firstInt==secondInt, which was also eliminated by your || secondInt == firstInt.

Rather than what you had, you probably want something like:

if(firstInt <= secondInt && firstInt <= thirdInt)
{
    System.out.println("The minimum is " + firstInt);    
} 
else if(secondInt <= thirdInt)
{
    System.out.println("The minimum is " + secondInt);    
} 
else
{
    System.out.println("The minimum is " + thirdInt);    
} 

   

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.