Question
import java.util.Scanner; Java: In the below code, Add the following: 1) Ask the user to continue or stop 2) If the user enter 0
import java.util.Scanner; Java: In the below code, Add the following:
1) Ask the user to continue or stop
2) If the user enter " 0 or negative number" print "Invalid Number-Please enter valid date"
The Code in JAVA:
import java.util.Scanner;
public class Zodiac {
public static void main(String[] args) {
// Attach scanner object to the file Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the day and the month of the birth System.out.print("Enter month: "); String month = scanner.nextLine(); System.out.print("Enter date: "); int date = scanner.nextInt(); zodiac_sign(date, month); scanner.close();
}
static void zodiac_sign(int day, String month) { String astro_sign = "";
// creating a 2D array of 12 rows and 2 columns // the first column stores the major zodiac the second column stores the minor zodiac
String zodiacs[][] = { { "Capricorn", "aquarius" }, { "Aquarius", "pisces" }, { "Pisces", "aries" }, { "Aries", "taurus" }, { "Taurus", "gemini" }, { "Gemini", "cancer" }, { "Cancer", "leo" }, { "Leo", "virgo" }, { "Virgo", "libra" }, { "Libra", "scorpio" }, { "Scorpio", "sagittarius" }, { "Sagittarius", "capricorn" }, { "Capricorn", "aquarius" } };
// Assigning the zodiac using the array
if (month.equalsIgnoreCase("december")) {
if (day < 22) astro_sign = zodiacs[11][0]; else astro_sign = zodiacs[11][1]; }
else if (month.equalsIgnoreCase("january")) { if (day < 20) astro_sign = zodiacs[0][0]; else astro_sign = zodiacs[0][1]; }
else if (month.equalsIgnoreCase("february")) { if (day < 19) astro_sign = zodiacs[1][0]; else astro_sign = zodiacs[1][1]; }
else if (month.equalsIgnoreCase("march")) { if (day < 21) astro_sign = zodiacs[2][0]; else astro_sign = zodiacs[2][1]; } else if (month.equalsIgnoreCase("april")) { if (day < 20) astro_sign = zodiacs[3][0]; else astro_sign = zodiacs[3][1]; }
else if (month.equalsIgnoreCase("may")) { if (day < 21) astro_sign = zodiacs[4][0]; else astro_sign = zodiacs[4][1]; }
else if (month.equalsIgnoreCase("june")) { if (day < 21) astro_sign = zodiacs[5][0]; else astro_sign = zodiacs[5][1]; }
else if (month.equalsIgnoreCase("july")) { if (day < 23) astro_sign = zodiacs[6][0]; else astro_sign = zodiacs[6][1]; }
else if (month.equalsIgnoreCase("august")) { if (day < 23) astro_sign = zodiacs[7][0]; else astro_sign = zodiacs[7][1]; }
else if (month.equalsIgnoreCase("september")) { if (day < 23) astro_sign = zodiacs[8][0]; else astro_sign = zodiacs[8][1]; }
else if (month.equalsIgnoreCase("october")) { if (day < 23) astro_sign = zodiacs[9][0]; else astro_sign = zodiacs[9][1]; }
else if (month.equalsIgnoreCase("november")) { if (day < 22) astro_sign = zodiacs[10][0]; else astro_sign = zodiacs[10][1]; }
if (astro_sign.isEmpty()) { astro_sign = "Invalid Input"; } // Display the result System.out.println(astro_sign); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Heres the modified code in Java import javautilScanner public clas...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started