11. 案例

买飞机票

import java.util.Scanner;

//买飞机票
public class demo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入原价,月份,类型");
        int money = sc.nextInt();
        int month = sc.nextInt();
        String type = sc.next();
        double rs = calc(money,month,type);
        System.out.println("最终价格是:" + rs);
    }

    public static double calc(double money, int month, String type) {
        if (month >= 5 && month <= 10) {
            switch (type) {
                case "经济舱":
                    money *= 0.85;
                    break;
                case "头等舱":
                    money *= 0.9;
                    break;
                default:
                    System.out.println("请输入正确仓位");
                    money = -1;
            }
        } else if (month == 11 || month == 12 || month <= 4 || month >= 1) {
            switch (type) {
                case "经济舱":
                    money *= 0.65;
                    break;
                case "头等舱":
                    money *= 0.7;
                    break;
                default:
                    System.out.println("请输入正确仓位");
                    money = -1;
            }
        } else {
            System.out.println("月份有问题");
            money = -1;
        }
        return money;
    }
}

找素数 

//找素数
public class demo {
    public static void main(String[] args) {
        for (int i = 101; i <= 200; i++) {
            boolean flag = true; //设置信号位
            for (int j = 2; j < i / 2; j++) {
                if (i % j == 0){
                    flag = false;
                    break;
                }
            }
            if (flag){
                System.out.print(i + "\t");
            }
        }
    }
}

开发验证码

import java.util.Random;

//开发验证码
public class demo {
    public static void main(String[] args) {
        String code = createCode(5);
        System.out.println("验证码为" + code);
    }

    public static String createCode(int n){
        Random r = new Random();
        String code = "";
        for (int i = 0; i < n; i++) {
            int type = r.nextInt(3);
            switch (type){
                case 0:
                   //大写字母A(65),Z(65+25)
                    char ch = (char) (r.nextInt(26) + 65);
                    code += ch;
                    break;
                case 1:
                    //小写字母A(97),Z(97+25)
                    char ch1 = (char) (r.nextInt(26) + 97);
                    code += ch1;
                    break;
                case 2:
                    code += r.nextInt(10);// 0-9
                    break;
            }
        }
        return code;
    }
}

数组元素复制

    public static void main(String[] args) {
        int[] arr1 = {0,1,2,3};
        int[] arr2 = new int[arr1.length];

        copy(arr1,arr2);

        printArray(arr1);
        printArray(arr2);
        
    }

    public static void copy(int[] arr1, int[] arr2){
        for (int i = 0; i < arr1.length; i++) {
            arr2[i] = arr1[i];
        }
    }

    public static void printArray(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

评委打分

    public static void main(String[] args) {
        int[] scores = new int[6];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++) {
            System.out.println("输入第" + (i + 1) + "个评委的分数");
            scores[i] = sc.nextInt();
        }

        int sum = 0;
        int max = scores[0];
        int min = scores[0];
        for (int i = 0; i < scores.length; i++) {
            if (max < scores[i]){
                max = scores[i];
            }
            if (min > scores[i]){
                min = scores[i];
            }
            sum += scores[i];
        }
        
        double result = (sum - max - min) * 1.0 / (scores.length - 2);
        System.out.println("平均分为 :" + result);
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值