[英]using user input in switch
import java.util.*;
public class assigment4number3
{
public static void main (String [] args)
{
int mon, tues, wed, thurs, fri;
mon=1; tues=2; wed=3;thurs=4;fri=5;
Scanner day = new Scanner(System.in);
System.out.println("Enter Day of week: mon, tues, weds, thurs, fri, sat, sun");
String week= day.next();
switch (week) {
case 1:
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case 2:
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case 3:
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case 4:
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case 5:
System.out.println("Discreet Math");
System.out.println("Philosophy");
break;
default:
System.out.println("Its the weekend");
break;
}
}
}
I'm trying to use input in my switch I'm writing a code where I can impute a day of the week and my schedule should pop up, but I'm having trouble using an input in my switch我正在尝试在我的开关中使用输入我正在编写一个代码,我可以在其中估算一周中的某一天,并且我的日程安排应该会弹出,但是我在我的开关中使用输入时遇到了问题
All of your case
labels are of type int
.您所有的
case
标签都是int
类型。 Change them to String
or switch
on an int
.将它们更改为
String
或switch
int
。 Like,喜欢,
int week = day.nextInt();
or或者
switch (week) {
case "1":
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case "2":
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case "3":
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case "4":
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case "5":
System.out.println("Discreet Math");
System.out.println("Philosophy");
break;
default:
System.out.println("Its the weekend");
break;
}
If you mean to use the values mon
- fri
instead of numbers, you could use those for your case
labels.如果您打算使用值
mon
- fri
而不是数字,您可以将这些用于您的case
标签。 Like,喜欢,
String week = day.next();
switch (week.toLowerCase()) {
case "mon":
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case "tues":
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case "weds":
System.out.println(" Discrete Math");
System.out.println("Philosophy");
System.out.println("Calculus");
break;
case "thurs":
System.out.println("Modern World");
System.out.println("Java Programming");
break;
case "fri":
System.out.println("Discreet Math");
System.out.println("Philosophy");
break;
default:
System.out.println("Its the weekend");
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.