2016-06-29 150 views
0

有沒有一種「更整潔」的方式來編寫這段代碼?我是一個簡單的堅持者,這對我來說似乎有點太重複了。 (我將日期改爲數字,以便我可以在自己的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(); 
} 

回答

2

如果你不使用JDK 1.8,這個代碼可以幫助你:

 List<String> strDays = Arrays.asList("Monday", "Tuesday", "Wednesday", "Thusday", "Friday", "Saturday", "Sunday"); 

    String weekday = scanText.nextLine(); 

    int day = 0; 
    if(strDays.contains(weekday)) { 

     day = strDays.indexOf(weekday) + 1; 
    } else { 
     System.out.print("Error! Invalid day: "); 
     weekday = scanText.nextLine(); 
    } 
0
import java.io.*; 
import java.text.*; 
import java.util.*; 


public class DayofWeekExample { 

    public static int getIntDayOfWeek(String dayOfWeek) 
    { 
     try 
     { 
      DateFormat formatter ; 
      Date date ; 
      formatter = new SimpleDateFormat("EEE"); 
      date = (Date)formatter.parse(dayOfWeek); 
      GregorianCalendar g = new GregorianCalendar(); 
      g.setTime(date); 
      return g.get(Calendar.DAY_OF_WEEK); 
     } 
     catch (ParseException e) 
     { 
      System.out.println("Exception :"+e); 
     } 
     return 0; 
    } 

    public static void main(String[] args) throws IOException { 
     // Beginnig day is Sunday 

     Scanner scanText = new Scanner(System.in); 

     System.out.print("Enter Day: "); 
     String weekday = scanText.nextLine(); 

     System.out.println("-> " + getIntDayOfWeek(weekday)); 


    } 
} 

一週的第一天,從當前區域設置的。如果您未設置日曆的區域設置(Calendar.getInstance(Locale)new GregorianCalendar(Locale)),則它將使用系統的默認設置。

+0

這很好,但我怎樣才能使用這個獲得用戶輸入,然後從特定的一天獲得一個數字? – user6512771

0

因爲你基本上是映射String值的int價值,我認爲,它的Map一個典型的使用情況。您可以編寫類似下面(一個缺點是,你必須初始化Map但可以被封裝爲更好的可讀性)

Scanner scanText = new Scanner(System.in); 

     System.out.print("Enter Day: "); 
     String weekday = scanText.nextLine(); 

     int day = 0; 
     Map<String,Integer> day_of_week=new HashMap<String,Integer>(); 
     day_of_week.put("MONDAY", 1); 
     day_of_week.put("TUESDAY", 2); 
     day_of_week.put("WEDNESDAY", 3); 
     day_of_week.put("THURSDAY", 4); 
     day_of_week.put("FRIDAY", 5); 
     day_of_week.put("SATURDAY", 6); 
     day_of_week.put("SUNDAY", 7); 

     if(day_of_week.containsKey(weekday.toUpperCase())){ 
      day = day_of_week.get(weekday.toUpperCase()).intValue(); 
     }else{ 
      System.out.print("Error! Invalid day: "); 
       weekday = scanText.nextLine(); 
     } 

我相信你一定以爲會比較部位從else塊讀值後再次。

+0

我在想這件事,我怎麼才能讓它回到if語句之後的else呢? – user6512771

+0

你將不得不把整個事情放在'while'或'do-while'循環中,並在你選擇的任何特殊的'String'處打斷。 Elliott的答案是[見此](http://stackoverflow.com/questions/20589178/i-have-to-make-a-loop-taking-a-users-input-until-done-is-entered)。而不是'weekday = scanText.nextLine();',你可以簡單地在'else'中說'繼續'。 –