Java 工作日输入验证

Java Weekday input Validation

是否有 "tidier" 编写此代码的方法?我是一个坚持简单的人,这对我来说似乎有点重复。 (我正在将日期更改为数字,以便我可以在他们自己的 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();
}
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), or new GregorianCalendar(Locale)),它将使用系统的默认设置。

如果您使用的不是 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();
     }

由于您基本上是将 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 块中读取值后一定会想到再次进入比较部分。