Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java工作日输入验证_Java_String_Dayofweek - Fatal编程技术网

Java工作日输入验证

Java工作日输入验证,java,string,dayofweek,Java,String,Dayofweek,是否有一种“更整洁”的方式来编写此代码?我坚持简单,这对我来说似乎有点太重复了。(我将天数改为数字,这样如果有人想知道的话,我可以在自己的if语句中使用它们)有什么建议吗 Scanner scanText = new Scanner(System.in); System.out.print("Enter Day: "); String weekday = scanText.nextLine(); int day = 0; if (weekday.equalsIgnorCase("Monda

是否有一种“更整洁”的方式来编写此代码?我坚持简单,这对我来说似乎有点太重复了。(我将天数改为数字,这样如果有人想知道的话,我可以在自己的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();
     }
List strDays=array.asList(“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”);
String weekday=scanText.nextLine();
整日=0;
if(标准日期包含(工作日)){
day=标准日期。indexOf(工作日)+1;
}否则{
System.out.print(“错误!无效日期:”);
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();
     }
List strDays=array.asList(“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”);
String weekday=scanText.nextLine();
整日=0;
if(标准日期包含(工作日)){
day=标准日期。indexOf(工作日)+1;
}否则{
System.out.print(“错误!无效日期:”);
weekday=scanText.nextLine();
}

由于基本上是将
字符串
值映射到
int
值,我认为这是
映射
的典型用例。您可以像下面这样编写(一个缺点是您必须初始化
映射
,但为了更好的可读性,可以对其进行封装)

Scanner scanText=新扫描仪(System.in);
系统输出打印(“输入日期:”;
String weekday=scanText.nextLine();
整日=0;
映射周中的日=新HashMap();
一周中的第二天(“星期一”,1);
星期二,2;
一周中的第二天(3号星期三);
一周中的第二天(星期四,4);
一周中的第二天(“星期五”,5);
星期六(6);
一周中的第二天。put(“星期日”,7);
if(周中的第天.containsKey(weekday.toUpperCase()){
day=day of_week.get(weekday.toUpperCase()).intValue();
}否则{
System.out.print(“错误!无效日期:”);
weekday=scanText.nextLine();
}

我敢肯定,在从
else
块中读取值之后,您一定想过再次进入比较部分

由于基本上是将
字符串
值映射到
int
值,我认为这是
映射
的典型用例。您可以像下面这样编写(一个缺点是您必须初始化
映射
,但为了更好的可读性,可以对其进行封装)

Scanner scanText=新扫描仪(System.in);
系统输出打印(“输入日期:”;
String weekday=scanText.nextLine();
整日=0;
映射周中的日=新HashMap();
一周中的第二天(“星期一”,1);
星期二,2;
一周中的第二天(3号星期三);
一周中的第二天(星期四,4);
一周中的第二天(“星期五”,5);
星期六(6);
一周中的第二天。put(“星期日”,7);
if(周中的第天.containsKey(weekday.toUpperCase()){
day=day of_week.get(weekday.toUpperCase()).intValue();
}否则{
System.out.print(“错误!无效日期:”);
weekday=scanText.nextLine();
}

我敢肯定,在从
else
块中读取值之后,您一定想过再次进入比较部分

这很好,但是我如何使用它来获取用户输入,然后从特定日期获取数字?这很好,但是我如何使用它来获取用户输入,然后从特定日期获取数字?我想知道,如何使它在else之后返回if语句的开头?您必须将整个内容放入
while
do while
循环中,并在您选择的任何特殊
字符串处断开。艾略特的回答。而不是
weekday=scanText.nextLine()
,您可以在
else
中简单地说
continue
。我想知道的是,如何使它在else之后回到if语句的开头?您必须将整个内容放入
while
do while
循环中,并在您选择的任何特殊
字符串处断开。艾略特的回答。而不是
weekday=scanText.nextLine()
,您只需在
else
中说
continue
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();
        }