My programming assignment is to write a Java program that will convert a given y
ID: 3549650 • Letter: M
Question
My programming assignment is to write a Java program that will convert a given year to the corresponding animal in the Chinese Zodiac. My only other instructions were to use a switch statement and %. The program should give the user a choice of running it again. Here is my source code. I know it is very wrong but I am too confused to figure it out. HELP!!!
import java.util.Scanner;
/** This program will convert a given year to the
* corresponding animal in the Chinese Zodiac.
*/
public class Zodiac
{
public static void main(String[] args)
{
String yearAnimal = "";
Scanner in = new Scanner(System.in);
//Get user information
System.out.print("Enter a year: ");
int userYear = in.nextInt();
//Determine Chinese Zodiac from year entered
int year = (1997 - userYear) % 12;
switch (year)
{
case 1:
if ((year == 1) || (year == -11))
yearAnimal = "Rat";
break;
case 2:
if (year == 0)
yearAnimal = "Ox";
break;
case 3:
if ((year == 11) || (year == -1))
yearAnimal = "Tiger";
break;
case 4:
if ((year == 10) || (year == -2))
yearAnimal = "Rabbit";
break;
case 5:
if ((year == 9) || (year == -3))
yearAnimal = "Dragon";
break;
case 6:
if ((year == 8) || (year == -4))
yearAnimal = "Dragon";
break;
case 7:
if ((year == 7) || (year == -5))
yearAnimal = "Horse";
break;
case 8:
if ((year == 6) || (year == -6))
yearAnimal = "Sheep";
break;
case 9:
if ((year == 5) || (year == -7))
yearAnimal = "Monkey";
break;
case 10:
if ((year == 4) || (year == -8))
yearAnimal = "Rooster";
break;
case 11:
if ((year == 3) || (year == -9))
yearAnimal = "Dog";
break;
case 12:
if ((year == 2) || (year == -10))
yearAnimal = "Pig";
break;
default:
System.out.println("Invalid entry.");
break;
}
//Give user Chinese Zodiac animal
System.out.println(userYear + " is the year of the " + yearAnimal);
//Ask if user would like to run the program again
System.out.print(" ");
System.out.print("Run again (Y/N): ");
String answer = in.nextLine();
if (answer.equalsIgnoreCase("y"));
{
System.out.println("Enter a year: ");
}
}
}
Explanation / Answer
I have just used a do-while loop so that the program runs as many times the user presses 'y' and it has to run atleast once, hence the do-while.
The variable "answer" is a string and I used compareToIgnoreCase() to see whether it has the vale of "y" and repeat the process accordingly.
One more thing to note that the case statements inside the switch(year) are actually referring to the value the variable "year" takes.
So, if I write
switch(year)
case 1:
do something...;
it means that if the variable "year" has value 1 ,then do something.
-----------------------------------------------------------------
/** This program will convert a given year to the
* corresponding animal in the Chinese Zodiac.
*/
import java.util.Scanner;
public class Zodiac
{
public static void main(String[] args)
{
String answer;
do{
String yearAnimal = "";
//Get user information
Scanner in = new Scanner(System.in);
System.out.print("Enter a year: ");
int userYear = in.nextInt();
//Determine Chinese Zodiac from year entered
int year = (1997 - userYear) % 12;
if(year<0)
year = year+12;
switch (year)
{
case 1:
yearAnimal = "Rat";
break;
case 0:
yearAnimal = "Ox";
break;
case 11 :
yearAnimal = "Tiger";
break;
case 10:
yearAnimal = "Rabbit";
break;
case 9:
yearAnimal = "Dragon";
break;
case 8:
yearAnimal = "Dragon";
break;
case 7:
yearAnimal = "Horse";
break;
case 6:
yearAnimal = "Sheep";
break;
case 5:
yearAnimal = "Monkey";
break;
case 4:
yearAnimal = "Rooster";
break;
case 3:
yearAnimal = "Dog";
break;
case 2:
yearAnimal = "Pig";
break;
default:
System.out.println("Invalid entry.");
break;
}
//Give user Chinese Zodiac animal
System.out.println(userYear + " is the year of the " + yearAnimal);
//Ask if user would like to run the program again
System.out.print("Run again (Y/N): ");
Scanner s = new Scanner(System.in);
answer = s.nextLine();
}while(answer.compareToIgnoreCase("y")==0);
//s1.compareToIgnoreCase(s2) function returns the difference between the ASCII values of the two strings in the order s1, s2.
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.