Java星座程序优化:无效输入时跳过后续输出语句的方法
Hey there! I see you're working on your Java assignment and want to tweak how the program handles invalid inputs—great question! The issue right now is that no matter if the input is valid or not, that final System.out.println("Your Sign is " + sign); runs every time, which gives an empty sign message when input is bad. Let's fix this without adding output statements to every if/else if block.
Simple Solutions to Try
Option 1: Use a Boolean Flag to Track Valid Input
We can add a small boolean variable to keep track of whether the input was valid. Then we only print the sign if the input passed all the checks.
Here's the modified code:
import java.util.Scanner; public class Sign { public static void main(String[] args) { Scanner input = new Scanner(System.in); int month, day; boolean isValidInput = true; // Flag to mark valid input System.out.println("What's your sign?"); System.out.print("Please enter the month of your birth (1-12): "); month = input.nextInt(); System.out.print("Please enter the day of your birth (1-31): "); day = input.nextInt(); String sign = ""; if ((month == 3 && (day >= 21 && day <= 31)) || (month == 4 && (day >= 1 && day <= 19))) { sign = "Aries"; } else if ((month == 4 && (day >= 20 && day <= 30)) || (month == 5 && (day >= 1 && day <= 20))) { sign = "Taurus"; } else if ((month == 5 && (day >= 21 && day <= 31)) || (month == 6 && (day >= 1 && day <= 21))) { sign = "Gemini"; } else if ((month == 6 && (day >= 22 && day <= 30)) ||(month == 7 && (day >= 1 && day <= 22))) { sign = "Cancer"; } else if ((month == 7 && (day >= 23 && day <= 31)) || (month == 8 && (day >= 1 && day <= 22))) { sign = "Leo"; } else if ((month == 8 && (day >= 23 && day <= 31)) || (month == 9 && (day >= 1 && day <= 22))) { sign = "Virgo"; } else if ((month == 9 && (day >= 23 && day <= 30)) || (month == 10 && (day >= 1 && day <= 22))) { sign = "Libra"; } else if ((month == 10 && (day >= 23 && day <= 31)) || (month == 11 && (day >= 1 && day <= 21))) { sign = "Scorpio"; } else if ((month == 11 && (day >= 22 && day <= 30)) || (month == 12 && (day >=1 && day <= 21))) { sign = "Sagittarius"; } else if ((month == 12 && (day >= 22 && day <= 31)) || (month == 1 && (day >= 1 && day <= 19))) { sign="Capricorn"; // Fixed a typo here! } else if ((month == 1 && (day >= 20 && day <= 31)) || (month == 2 && (day >= 1 && day <= 18))) { sign="Aquarius"; } else if((month == 2 && (day >= 19 && day <= 29)) || (month == 3 && (day >= 1 && day <= 20))) { sign="Pisces"; // Fixed another typo here! } else { System.out.println("Invalid Entry. Please run the program to try again."); isValidInput = false; // Mark input as invalid } // Only print the sign if input was valid if (isValidInput) { System.out.println("Your Sign is " + sign); } input.close(); } }
Quick note: I fixed two small typos in your original code—"Capricon" → "Capricorn" and "Pieces" → "Pisces"—just to make the sign names accurate!
Option 2: Exit Early in the Else Block
Another straightforward fix is to use return in the else block to exit the main method right after showing the error message. This skips all the code that comes after, including the sign print statement. Just don't forget to close the scanner before returning:
// ... (same code up to the else block) else { System.out.println("Invalid Entry. Please run the program to try again."); input.close(); // Close the scanner first return; // Exit the main method immediately } System.out.println("Your Sign is " + sign); input.close();
Why These Work
- Boolean Flag: This keeps your code organized and gives you flexibility if you want to add more logic later (like re-prompting for input instead of exiting).
- Early Return: This is a quick, no-fuss fix that directly stops execution when input is invalid—perfect for a beginner program.
Either approach will solve your problem without having to duplicate output statements in every if/else if branch. 😊
内容的提问来源于stack exchange,提问作者Melanie Fars

