Write a program that asks the user to enter a year. The program should then indi
ID: 3820430 • Letter: W
Question
Write a program that asks the user to enter a year. The program should then indicate the type of year it is based on the Chinese Zodiac. The order of the Chinese Zodiac shown below is based on a 12-year cycle. The first zodiac corresponds to years divisible by 12 (remainder 0), the second year corresponds to years that a produce a remainder of 1 and so on. For instance, 2017 is the year of the rooster because 2017 divided by 12 produces a remainder of 1. Monkey Rooster Dog Pig Rat Ox Tiger Rabbit Dragon Snake Horse Sheep You will need: 1. A scanner object for input 2. A variable to store the year entered 3. A switch statement to select the appropriate zodiac sign 4. Comments for appropriate documentation A sample of the output is shown below: Enter a year: 1979 The year 1979 is the year of the sheepExplanation / Answer
ChineeseZodiac.java
import java.util.Scanner;
public class ChineeseZodiac {
public static void main(String[] args) {
//Declaring variables
int index;
int year;
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
//Declaring an String array and initialize with year names
String zodiac[] = { "Monkey", "Rooster", "Dog", "Pig", "Rat", "Ox",
"Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Sheep" };
//Getting the year entered by the user
System.out.print("Enter a year (Negative number ends the program ):");
year = sc.nextInt();
/* This loop continues to execute until the user
* enters a negative number
* */
while (year >= 0) {
switch (year % 12) {
case 0: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
case 1: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
case 2: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
case 3: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
case 4: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
case 5: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
case 6: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
case 7: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
case 8: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
case 9: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
case 10: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
case 11: {
System.out.println(year+" is the year of "+zodiac[year%12]);
break;
}
}
System.out.print(" Enter a year ,negative number ends the program :");
year = sc.nextInt();
}
}
}
_________________
Output:
Enter a year (Negative number ends the program ):1979
1979 is the year of Sheep
Enter a year ,negative number ends the program :1983
1983 is the year of Pig
Enter a year ,negative number ends the program :-1
________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.