如何让Java星座程序中if-else分支触发后重新执行if判断逻辑?
Hey there! The problem here is that your program stops right after showing the invalid date message. To make it ask the user for input again until they provide a valid birth date, you just need to wrap your input and validation logic in a loop. Here's a straightforward fix using a do-while loop (perfect for this scenario since we want to run the input process at least once):
Modified Code
import java.util.Scanner; public class Horoscope { public Horoscope() { Scanner sc = new Scanner(System.in); String invalidMsg = "Please enter a valid date"; int month; int day; boolean isInputValid; // Tracks if the entered date is valid do { isInputValid = true; // Assume valid initially; flip if invalid later System.out.println("What is your month of birth?"); month = sc.nextInt(); System.out.println("What is your day of birth?"); day = sc.nextInt(); // Existing zodiac logic if((month == 3 && day >= 20 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) { System.out.println("You are an Aries"); } else if((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20)) { System.out.println("You are a Taurus"); } else if((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 20)) { System.out.println("You are a Gemini"); } else if((month == 6 && day >= 21 && day <= 30) || (month == 7 && day >= 1 && day <= 22)) { System.out.println("You are a Cancer"); } else if((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22)) { System.out.println("You are a Leo"); } else if((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22)) { System.out.println("You are a Virgo"); } else if((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 22)) { System.out.println("You are a Libra"); } else if((month == 10 && day >= 23 && day <= 30) || (month == 11 && day >= 1 && day <= 21)) { System.out.println("You are a Scorpio"); } else if((month == 11 && day >= 22 && day <= 31) || (month == 12 && day >= 1 && day <= 21)) { System.out.println("You are a Sagittarius"); } else if((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)) { System.out.println("You are a Capricorn"); } else if((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 18)) { System.out.println("You are an Aquarius"); } else if((month == 2 && day >= 19 && day <= 29) || (month == 3 && day >= 1 && day <= 20)) { System.out.println("You are a Pisces"); } else { System.out.println(invalidMsg); isInputValid = false; // Mark input as invalid to trigger another loop iteration } } while (!isInputValid); // Repeat loop until valid input is received sc.close(); // Clean up the Scanner resource } public static void main(String[] args) { new Horoscope(); // Added main method to run the program } }
Key Changes Explained
- Loop Structure: The
do-whileloop ensures the input/validation process runs at least once, and repeats as long as the user enters an invalid date. - Validation Flag: The
isInputValidboolean tracks whether the entered date matches a valid zodiac range. We set it tofalseonly in the else (invalid) branch, which tells the loop to run again. - Resource Cleanup: Added
sc.close()to properly close the Scanner, and amainmethod so you can actually run the program.
内容的提问来源于stack exchange,提问作者user12137309
相关产品推荐
相关产品推荐

