dimanche 19 novembre 2017

if statement only recognizing certain integers in Java

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter value: ");
    int value = input.nextInt();

    if (value == 1 || value == 3 || value == 5 ||value == 7 || value == 9) {
        if (value % 3 == 0)
            System.out.println(value + " is an odd number less than 10"
                    + " and divisible by 3");
    }
    else if (value == 2 || value == 4 || value == 6 ||value == 8) {
        if (value % 3 == 0)
            System.out.println(value + " is an even number less than 10"
                    + " and divisible by 3");
    }
    else
        System.out.println("Invalid number");
    input.close();
}

if-else statement only recognizes integers that are divisible by 3 which are 3, 6 or 9. When I type 1 or 5, for example, the programme does not display any of the input and just stop running.

Aucun commentaire:

Enregistrer un commentaire