需求:输入星期数,显示今天的减肥活动
周一:跑步
周二:游泳
周三:慢走
周四:动感单车
周五:拳击
周六:爬山
周日:好好吃一顿
代码实现:
if版
import java.util.Scanner;
public class MethodDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入星期数:");
int day = sc.nextInt();
loseWeight(day);
}
public static void loseWeight(int day) {
if(day>=1&& day<=7){
if (day == 1) {
System.out.println("今日计划:跑步");
} else if (day == 2) {
System.out.println("今日计划:游泳");
} else if (day == 3) {
System.out.println("今日计划:慢走");
} else if (day == 4) {
System.out.println("今日计划:动感单车");
} else if (day == 5) {
System.out.println("今日计划:拳击");
} else if (day == 6) {
System.out.println("今日计划:爬山");
} else {
System.out.println("今日计划:好好吃一顿");
}}
else
{
System.out.print("输入错误!");
}
}
switch版
import java.util.Scanner;
public class MethodDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入星期数:");
int day = sc.nextInt();
loseWeight(day);
}
public static void loseWeight(int day) {
switch (day) {
case 1:
System.out.println("今日计划:跑步");
break;
case 2:
System.out.println("今日计划:游泳");
break;
case 3:
System.out.println("今日计划:慢走");
break;
case 4:
System.out.println("今日计划:动感单车");
break;
case 5:
System.out.println("今日计划:拳击");
break;
case 6:
System.out.println("今日计划:爬山");
break;
case 7:
System.out.println("今日计划:好好吃一顿");
break;
default:
System.out.print("输入错误");
}
}
}
|