if... else if...else语句练习
程序员文章站
2024-01-06 14:38:46
...
需求:键盘录入一个星期数(1,2,3.....7),输出对应的星期一,星期二......星期日。
import java.util.Scanner;
public class Week{
public static void main(String[] args){
System.out.print("请输入一个星期数(1-7):");
//从键盘录入星期数
Scanner sc1 = new Scanner(System.in);
int week = sc1.nextInt();
if( week == 1){
System.out.print("星期一");
}else if( week == 2){
System.out.print("星期二");
}else if( week == 3){
System.out.print("星期三");
}else if( week == 4){
System.out.print("星期四");
}else if( week == 5){
System.out.print("星期五");
}else if( week == 6){
System.out.print("星期六");
}else if( week == 7){
System.out.print("星期日");
}else{
System.out.println("请输入有效的星期数");
}
}
}
结果