in JAVA Write an astrology program. The user will give their birthday as a month
ID: 3767106 • Letter: I
Question
in JAVA
Write an astrology program. The user will give their birthday as a month number (1 – 12) and then a day number (1 – 31). Your program will then output the person's astrological sign on one line, and then a horoscope for the user on the next. You may make up whatever horoscope you want for each different sign. Here are the sign dates:
Sign
Start Date
End Date
Aries
March 21
April 19
Taurus
April 20
May 20
Gemini
May 21
June 21
Cancer
June 22
July 22
Leo
July 23
August 22
Virgo
August 23
September 22
Libra
September 23
October 22
Scorpio
October 23
November 21
Sagittarius
November 22
December 21
Capricorn
December 22
January 19
Aquarius
January 20
February 18
Pisces
February 19
March 20
and here is an example execution:
Enter your birth month (1-12): 9
Enter your birth day (1-31): 26
You are a Libra!
Tony Stark will buy out your business.
You are going to need to use a long set of if and else if statements. You will either need to use compound expressions (combining expressions with the “and” and “or” operators) or have nested if statements (if statements inside of if statements).
To get started, try to get one or two of the signs correct first, then try to make it work for the rest. You should verify that the month is between 1 and 12 and the day is between 1 and 31, but you don't need to check the day for specific months.
Sign
Start Date
End Date
Aries
March 21
April 19
Taurus
April 20
May 20
Gemini
May 21
June 21
Cancer
June 22
July 22
Leo
July 23
August 22
Virgo
August 23
September 22
Libra
September 23
October 22
Scorpio
October 23
November 21
Sagittarius
November 22
December 21
Capricorn
December 22
January 19
Aquarius
January 20
February 18
Pisces
February 19
March 20
Explanation / Answer
import java.util.Scanner;
public class Astrology {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your birth month (1-12): ");
int month = input.nextInt();
System.out.print("Enter your birth day (1-31): ");
int day = input.nextInt();
input.close();
String sign;
if (month < 1 || month > 12) {
System.out.println("Invalid month number. Months are between 1-12");
return;
}
else if (day < 1 || day > 31) {
System.out.println("Invalid day number. Days are between 1-31");
return;
}
else if (month == 1) {
if (day <= 19) {
sign = "Capricorn";
} else {
sign = "Aquarius";
}
} else if (month == 2) {
if (day <= 18) {
sign = "Aquarius";
} else {
sign = "Pisces";
}
} else if (month == 3) {
if (day <= 20) {
sign = "Pisces";
} else {
sign = "Aries";
}
} else if (month == 4) {
if (day <= 19) {
sign = "Aries";
} else {
sign = "Taurus";
}
} else if (month == 5) {
if (day <= 20) {
sign = "Taurus";
} else {
sign = "Gemini";
}
} else if (month == 6) {
if (day <= 21) {
sign = "Gemini";
} else {
sign = "Cancer";
}
} else if (month == 7) {
if (day <= 22) {
sign = "Cancer";
} else {
sign = "Leo";
}
} else if (month == 8) {
if (day <= 22) {
sign = "Leo";
} else {
sign = "Virgo";
}
} else if (month == 9) {
if (day <= 22) {
sign = "Virgo";
} else {
sign = "Libra";
}
} else if (month == 10) {
if (day <= 23) {
sign = "Libra";
} else {
sign = "Scorpio";
}
} else if (month == 11) {
if (day <= 21) {
sign = "Scorpio";
} else {
sign = "Sagittarius";
}
} else {
if (day <= 21) {
sign = "Sagittarius";
} else {
sign = "Capricorn";
}
}
System.out.println("You are a " + sign);
String fortune;
if (sign.equals("Capricorn")) {
fortune = "Example Capricorn fortune";
} else if (sign.equals("Aquarius")) {
fortune = "Example Aquarius fortune";
} else if (sign.equals("Pisces")) {
fortune = "Example Pisces fortune";
} else if (sign.equals("Aries")) {
fortune = "Example Aries fortune";
} else if (sign.equals("Taurus")) {
fortune = "Example Taurus fortune";
} else if (sign.equals("Gemini")) {
fortune = "Example Gemini fortune";
} else if (sign.equals("Cancer")) {
fortune = "Example Cancer fortune";
} else if (sign.equals("Leo")) {
fortune = "Example Leo fortune";
} else if (sign.equals("Virgo")) {
fortune = "Example Virgo fortune";
} else if (sign.equals("Libra")) {
fortune = "Example Libra fortune";
} else if (sign.equals("Scorpio")) {
fortune = "Example Scorpio fortune";
} else {
fortune = "Unknown sign. You're a special one.";
}
System.out.println(fortune);
}
}
OUTPUT:
Enter your birth month (1-12): 9
Enter your birth day (1-31): 26
You are a Libra
Example Libra fortune
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.