mardi 24 avril 2018

if statements where x can equal a series of numbers/a range of numbers?

Trying to write a program for a combination lock, where if the inputted number is within 3 numbers of the correct number it will state that the lock is open. (i.e if the first number of the lock combination is 5, it will state that the lock is open if someone inputs 2, 3, 4, 5, 6, 7, or 8.)

This is the code I have so far (I'm only trying to figure out how to get the program to recognize whether or not the first number is correct at the moment, if you're wondering why there is a second + third input.)

    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter the first number: ");
    int first = keyboard.nextInt();
    System.out.print("Enter the second number: ");
    int second = keyboard.nextInt();
    System.out.print ("Enter the third number: ");
    int third = keyboard.nextInt();
    if (first == )
        System.out.println("It works!");
    else
        System.out.println("It didn't work");
}

Aucun commentaire:

Enregistrer un commentaire