
package com.itheima.demo;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
// 目标:完成买飞机票的价格计算
// 1. 让用户输入机票原价,月份,仓位类型
Scanner sc = new Scanner(System.in);
System.out.println("请输入机票原价:");
double money = sc.nextInt(); // 先写sc.nextInt 然后按ctrl + alt + v会自动定义变量
System.out.println("请输入月份:");
int mooth = sc.nextInt();
System.out.println("请输入仓位类型:");
String type = sc.next();
// 调用方法
System.out.println(calc(money,mooth,type));
}
// 定义方法
public static double calc(double money, int month, String type){
if (month >= 5 && month <= 10){
// 这样是旺季
// 然后判断是否仓位类型
switch (type){
case "头等仓":
money *= 0.9;
break;
case "经济仓":
money *= 0.85;
break;
default:
System.out.println("你输入的数据有误");
money = -1; // 返回-1,代表数据有误
}
}else if (month == 11 || month == 12 || (month >= 1 && month<= 4)) {
switch (type){
case "头等仓":
money *= 0.7;
break;
case "经济仓":
money *= 0.65;
break;
default:
System.out.println("你输入的数据有误");
money = -1;
}
} else { // 这里的else表示,当if 和elseif不满足时,输出
System.out.println("你输入的月份有误");
money = -1;
}
// 这里要有最终的返回值
return money; // 返回最终的价格
}
}