Question: Here below is the instructions and my code from HW5. Modify the fortune teller program from Homework #5. All requirements of Homework #5 apply. In
Here below is the instructions and my code from HW5.
Modify the fortune teller program from Homework #5. All requirements of Homework #5 apply. In addition, make the following changes described below.
Create a String array of 5 random fortunes for Red.
Create a String array of 5 random fortunes for Green.
Create a String array of 5 random fortunes for Blue.
When the program chooses a fortune based on the color picked by the user, generate a random number from 0-4. Use this to index into the String array corresponding to the color the user picked. Output the fortune at that index in the array.
My code:
import java.util.Scanner;
public class FortuneTeller {
public static void main(String[] args) {
String name; // name of a person
int month, day, color, zodiac; // bday and color
Scanner reader = new Scanner(System.in);
// user input for name and welcome message
System.out.print("What is your name? ");
name = reader.nextLine();
System.out.println("Welcome " + name + ".");
// user input for month and error message if month is incorrect
while (true) {
System.out.print("What month were you born on (1-12)? ");
month = reader.nextInt();
if (month < 1 || month > 12) {
System.out.println("** Invalid .Must be between 1-12 **");
continue;
} else
break;
}// while statement
// user input for day and get integer of the zodiac sign in variable zodiac
while (true) {
System.out.print("What day of the month were you born on (1-31)? ");
day = reader.nextInt();
if (day < 1 || day > 31) {
System.out.println("** Invalid.Must be between 1-31 **");
continue;
} else
break;
}//while
zodiac = GetZodiac(month, day);
System.out.print(name + ", I see your sign is ");
switch (zodiac) {
case 1:
System.out.println("Aries");
break;
case 2:
System.out.println("Taurus");
break;
case 3:
System.out.println("Gemini");
break;
case 4:
System.out.println("Cancer");
break;
case 5:
System.out.println("Leo");
break;
case 6:
System.out.println("Virgo");
break;
case 7:
System.out.println("Libra");
break;
case 8:
System.out.println("Scorpio");
break;
case 9:
System.out.println("Sagittarius");
break;
case 10:
System.out.println("Capricorn");
break;
case 11:
System.out.println("Aquarius");
break;
case 12:
System.out.println("Pisces");
break;
}// switch
// user input for color
while (true) {
System.out.print("Choose your favorite color (1=red, 2=green, 3=blue): ");
color = reader.nextInt();
if (color < 1 || color > 3) {
System.out.println("** Invalid.Must be between 1-3 **");
continue;
} else
break;
}//while
PrintFortune(color);
}// main method
private static void PrintFortune(int color) {
switch (color) {
case 1:
System.out.println("You will become a gold miner.");
break;
case 2:
System.out.println("You will become a forest ranger.");
break;
case 3:
System.out.println("You will sing the blues.");
break;
}//switch
}//printfortune
private static int GetZodiac(int month, int day) {
int num = 0;
if ((month == 3 && day >= 21 && day <= 31) || (month == 4 && day > 0 && day <= 19))
num = 1; // 1 for Aries
else if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day > 0 && day <= 20))
num = 2; // 2 for Taurus
else if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day > 0 && day <= 20))
num = 3; // 3 for Gemini
else if ((month == 6 && day >= 21 && day <= 30) || (month == 7 && day > 0 && day <= 22))
num = 4; // 4 for Cancer
else if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day > 0 && day <= 22))
num = 5; // 5 for Leo
else if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day > 0 && day <= 22))
num = 6; // 6 for Virgo
else if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day > 0 && day <= 22))
num = 7; // 7 for Libra
else if ((month == 10 && day >= 23 && day <= 31) || (month == 11 && day > 0 && day <= 21))
num = 8; // 8 for Scorpio
else if ((month == 11 && day >= 22 && day <= 30) || (month == 12 && day > 0 && day <= 21))
num = 9; // 9 for Sagittarius
else if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day > 0 && day <= 19))
num = 10; // 10 for Capricorn
else if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day > 0 && day <= 18))
num = 11; // 11 for Aquarius
else if ((month == 2 && day >= 19 && day <= 29) || (month == 3 && day > 0 && day <= 20))
num = 12; // 12 for Pisces
return num;
}//getzodiac
}// class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
