import java.util.Scanner;
public class Temp {
public String getWeek(int i) {
switch(i) {
case 1: return "Monday";
case 2: return "Tuesday";
case 3: return "Wednesday;";
case 4: return "Thursday";
case 5: return "Friday";
case 6: return "Saturday";
case 7: return "Sunday";
default: return "Unknown week";
}
}
public static void main(String[] args) {
Temp t = new Temp();
Scanner scanner = new Scanner(System.in);//输入1-7
System.out.println(t.getWeek(scanner.nextInt()));//显示对应星期
}
}
直接写一个方法啊
private String showDay(int i){
String today = null;
switch(i){
case 1:
today = "星期一";
case 2:
today = "星期二";
case 3:
today = "星期三";
case 4:
today = "星期四";
case 5:
today = "星期五";
case 6:
today = "星期六";
case 7:
today = "星期日";
}
return today;
}
这个貌似不难吧!写个switch(表达式)
{
case 常量表达式1:语句1;当参数i=1 时 打印周一 以下类同!
....
case 常量表达式2:语句2;
default:语句; 这个意思是如果输入的不是1—7那么你可以打印一个输入了无效数据
}语句就搞定了 望采纳,谢谢!
将1~7 和对应的星期放到map对象中,然后,用if ... else ...判断,或者switch case语句分析,根据输入码,从map中取值输出
if 和switch都行