有沒有一種「更整潔」的方式來編寫這段代碼?我是一個簡單的堅持者,這對我來說似乎有點太重複了。 (我將日期改爲數字,以便我可以在自己的if語句中使用它們,如果有人想知道)有什麼建議嗎?Java平日輸入驗證
Scanner scanText = new Scanner(System.in);
System.out.print("Enter Day: ");
String weekday = scanText.nextLine();
int day = 0;
if (weekday.equalsIgnorCase("Monday"))
day = 1;
else if (weekday.equalsIgnorCase("Tuesday"))
day = 2;
else if (weekday.equalsIgnorCase("Wednesday"))
day = 3;
else if (weekday.equalsIgnorCase("Thursday"))
day = 4;
else if (weekday.equalsIgnorCase("Friday"))
day = 5;
else if (weekday.equalsIgnorCase("Saturday"))
day = 6;
else if (weekday.equalsIgnorCase("Sunday"))
day = 7;
else {
System.out.print("Error! Invalid day: ");
weekday = scanText.nextLine();
}
這很好,但我怎樣才能使用這個獲得用戶輸入,然後從特定的一天獲得一個數字? – user6512771