簡體   English   中英

Java Weekday輸入驗證

[英]Java Weekday input Validation

有沒有一種“簡潔的”方式來編寫此代碼? 我追求簡單,這對我來說似乎有點重復。 (如果有人想知道的話,我將日期更改為數字,以便可以在自己的if語句中使用它們)。

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();
}

如果您不使用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();
     }
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) ),它將使用系統的默認設置。

因為您基本上是將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塊讀取值之后,您一定想過要再次進行比較。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM