16:根据输入的值,判断是星期几。(分别用if语句和switch语句实现)
输入:1
输出:星期1
- import java.util.Scanner;
- class HomeWork
- {
- public static void main(String[] args)
- { Scanner sc = new Scanner(System.in);
- System.out.println("此方法是根据数值,判断星期的程序。请输入一个整数:(每人只有3次机会哦)");
- for (int i =3; i > 0; i--)
- {
- int week = sc.nextInt();
- //ifRun(week);
- switchRun(week);
- System.out.println("您还有"+(i-1)+"次机会哦!");
- }
-
- }
-
- static void ifRun(int week)
- {
- if (week <=7 && week > 0)
- {
- if (week == 1)
- {
- System.out.println(week+"是星期一");
- }
- else if (week == 2)
- {
- System.out.println(week+"是星期二");
- }
- else if (week == 3)
- {
- System.out.println(week+"是星期三");
- }
- else if (week == 4)
- {
- System.out.println(week+"是星期四");
- }
- else if (week == 5)
- {
- System.out.println(week+"是星期五");
- }
- else if (week == 6)
- {
- System.out.println(week+"是星期六");
- }
- else
- System.out.println(week+"是星期日");
- }
- else
- System.out.println("抱歉,没有对应的星期,请输入1-7以内的数值,请重新输入");
-
- }
- static void switchRun(int week)
- {
- switch (week)
- {
- case 1:
- System.out.println(week+"是星期一");
- break;
- case 2:
- System.out.println(week+"是星期二");
- break;
- case 3:
- System.out.println(week+"是星期三");
- break;
- case 4:
- System.out.println(week+"是星期四");
- break;
- case 5:
- System.out.println(week+"是星期五");
- break;
- case 6:
- System.out.println(week+"是星期六");
- break;
- case 7:
- System.out.println(week+"是星期七");
- break;
- default:
- System.out.println("抱歉,没有对应的星期,请输入1-7以内的数值,请重新输入------------");
-
- }
- }
-
- }
复制代码
|
|