image.png

    1. package com.itheima.demo;
    2. import java.util.Scanner;
    3. public class Test1 {
    4. public static void main(String[] args) {
    5. // 目标:完成买飞机票的价格计算
    6. // 1. 让用户输入机票原价,月份,仓位类型
    7. Scanner sc = new Scanner(System.in);
    8. System.out.println("请输入机票原价:");
    9. double money = sc.nextInt(); // 先写sc.nextInt 然后按ctrl + alt + v会自动定义变量
    10. System.out.println("请输入月份:");
    11. int mooth = sc.nextInt();
    12. System.out.println("请输入仓位类型:");
    13. String type = sc.next();
    14. // 调用方法
    15. System.out.println(calc(money,mooth,type));
    16. }
    17. // 定义方法
    18. public static double calc(double money, int month, String type){
    19. if (month >= 5 && month <= 10){
    20. // 这样是旺季
    21. // 然后判断是否仓位类型
    22. switch (type){
    23. case "头等仓":
    24. money *= 0.9;
    25. break;
    26. case "经济仓":
    27. money *= 0.85;
    28. break;
    29. default:
    30. System.out.println("你输入的数据有误");
    31. money = -1; // 返回-1,代表数据有误
    32. }
    33. }else if (month == 11 || month == 12 || (month >= 1 && month<= 4)) {
    34. switch (type){
    35. case "头等仓":
    36. money *= 0.7;
    37. break;
    38. case "经济仓":
    39. money *= 0.65;
    40. break;
    41. default:
    42. System.out.println("你输入的数据有误");
    43. money = -1;
    44. }
    45. } else { // 这里的else表示,当if 和elseif不满足时,输出
    46. System.out.println("你输入的月份有误");
    47. money = -1;
    48. }
    49. // 这里要有最终的返回值
    50. return money; // 返回最终的价格
    51. }
    52. }