'F' → Fullscreen
if, else if, and else statements to create mutually exclusive conditions. birthYear % 12, which gives you a remainder between 0 and 11.
else if copy and paste it to speed things up, just tweak the values as needed. import java.util.Scanner;
public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in);
System.out.println("--- Chinese Zodiac Calendar ---"); System.out.print("Enter your birth year: ");
int birthYear = input.nextInt(); int zodiacNumber = birthYear % 12; String zodiacAnimal; // Assign animal based on birth year
input.nextLine(); // Consume newline
if (zodiacNumber == 0) { zodiacAnimal = "monkey"; } else if (zodiacNumber == 1) { zodiacAnimal = "rooster"; } else if (zodiacNumber == 2) { zodiacAnimal = "dog"; } else if (zodiacNumber == 3) { zodiacAnimal = "pig"; } else if (zodiacNumber == 4) { zodiacAnimal = "rat"; } else if (zodiacNumber == 5) { zodiacAnimal = "ox"; } else if (zodiacNumber == 6) { zodiacAnimal = "tiger"; } else if (zodiacNumber == 7) { zodiacAnimal = "rabbit"; } else if (zodiacNumber == 8) { zodiacAnimal = "dragon"; } else if (zodiacNumber == 9) { zodiacAnimal = "snake"; } else if (zodiacNumber == 10) { zodiacAnimal = "horse"; } else if (zodiacNumber == 11) { zodiacAnimal = "sheep"; } else { System.out.println("Error: invalid input"); zodiacAnimal = "something went wrong"; }
System.out.println("\nBirth year: " + birthYear + " - You are the year of the ..." + zodiacAnimal + "!");
System.out.println("Press enter to continue..."); input.nextLine(); // -----------------------------------------------------------------------------// }}import java.util.Scanner;
public class Program { public static void main(String[] args) {57 collapsed lines
Scanner input = new Scanner(System.in);
System.out.println("--- Chinese Zodiac Calendar ---"); System.out.print("Enter your birth year: ");
int birthYear = input.nextInt(); int zodiacNumber = birthYear % 12; String zodiacAnimal; // Assign animal based on birth year
input.nextLine(); // Consume newline
if (zodiacNumber == 0) { zodiacAnimal = "monkey"; } else if (zodiacNumber == 1) { zodiacAnimal = "rooster"; } else if (zodiacNumber == 2) { zodiacAnimal = "dog"; } else if (zodiacNumber == 3) { zodiacAnimal = "pig"; } else if (zodiacNumber == 4) { zodiacAnimal = "rat"; } else if (zodiacNumber == 5) { zodiacAnimal = "ox"; } else if (zodiacNumber == 6) { zodiacAnimal = "tiger"; } else if (zodiacNumber == 7) { zodiacAnimal = "rabbit"; } else if (zodiacNumber == 8) { zodiacAnimal = "dragon"; } else if (zodiacNumber == 9) { zodiacAnimal = "snake"; } else if (zodiacNumber == 10) { zodiacAnimal = "horse"; } else if (zodiacNumber == 11) { zodiacAnimal = "sheep"; } else { System.out.println("Error: invalid input"); zodiacAnimal = "something went wrong"; }
System.out.println("\nBirth year: " + birthYear + " - You are the year of the ..." + zodiacAnimal + "!");
System.out.println("Press enter to continue..."); input.nextLine(); // -----------------------------------------------------------------------------
System.out.println("\n--- Lottery Game ---\n"); System.out.println("Rules: Pick two lottery numbers ranging from 0 to 9"); System.out.println("Match two numbers and win BIG!"); System.out.println("Match one number in order and win SMALL!\n");
System.out.print("Enter your 1st lottery number (0 to 9): "); int lottoNum1 = input.nextInt();
System.out.print("Enter your 2nd lottery number (0 to 9): "); int lottoNum2 = input.nextInt();
int winningNum1 = (int)(Math.random() * 10); int winningNum2 = (int)(Math.random() * 10);
// Exit the program if either lotto number is out of range if (lottoNum1 < 0 || lottoNum1 > 9 || lottoNum2 < 0 || lottoNum2 > 9) { System.out.println("Error: Your lotto numbers are invalid. Exiting"); System.exit(0); // Exit application }
System.out.println("The winning numbers are " + winningNum1 + " and " + winningNum2);
if (lottoNum1 == winningNum1 && lottoNum2 == winningNum2) { System.out.println("You win BIG!"); } else if (lottoNum1 == winningNum1 || lottoNum2 == winningNum2) { System.out.println("You win SMALL!"); } else { System.out.println("You lose! Better luck next time!"); }// }}else if statement if the lotto numbers match out of order.
Your program output should something similar to the sample output below.
Enter your birth year: 1990
Birth year: 1990 - You are the year of the ...horse!Press enter to continue...
--- Lottery Game ---
Rules: Pick two lottery numbers ranging from 0 to 9Match two numbers and win BIG!Match one number in order and win SMALL!Enter your 1st lottery number (0 to 9): 7Enter your 2nd lottery number (0 to 9): 2The winning numbers are 6 and 7You lose! Better luck next time!You may write your reflection answers as comments at the bottom of your code.
if, else if, else change the behavior of the program compared to using multiple, separate
if statements? Describe a scenario where a separate if statement would be appropriate.
else block handles the case where the input is invalid. How does this
demonstrate the role of the else statement as a "catch-all" or "fallback" in your code?
Submit your activity and reflection answers to the appropriate dropbox.