samedi 27 juin 2020

What is the problem with my else if statements?

I'm doing this code for a class, which consists on finding the lowest integer inserted by a user. However, 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);    
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire