Solution: Ask Favorite Number

  • import java.util.Scanner; – imports the Scanner class to read user input.

  • public class Main – defines the class Main.

  • public static void main(String[] args) – main method where the program begins.

  • Scanner input = new Scanner(System.in); – creates a Scanner to take input from the keyboard.

  • System.out.print("What's your favorite number? "); – asks the user for their favorite number.

  • int number = input.nextInt(); – reads the number entered by the user.

  • if (number == 7) – checks if the number is 7, then prints "Lucky!".

  • else if (number % 2 == 0) – checks if the number is even, then prints "Nice and even.".

  • else – runs if neither condition is true, printing "Interesting choice.".

  • input.close(); – closes the Scanner to free resources.

Solution: Ask Favorite Number

  • import java.util.Scanner; – imports the Scanner class to read user input.

  • public class Main – defines the class Main.

  • public static void main(String[] args) – main method where the program begins.

  • Scanner input = new Scanner(System.in); – creates a Scanner to take input from the keyboard.

  • System.out.print("What's your favorite number? "); – asks the user for their favorite number.

  • int number = input.nextInt(); – reads the number entered by the user.

  • if (number == 7) – checks if the number is 7, then prints "Lucky!".

  • else if (number % 2 == 0) – checks if the number is even, then prints "Nice and even.".

  • else – runs if neither condition is true, printing "Interesting choice.".

  • input.close(); – closes the Scanner to free resources.