JavaSE 基础案例实战和代码

案例1: 买飞机票
在这里插入图片描述

package practice;

import java.util.Scanner;

public class PlanTickets {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Original Price:");
        double price = sc.nextDouble();
        System.out.println("month:");
        int month = sc.nextInt();
        System.out.println("type:");
        String type = sc.next();

        double result = calcPrice(price, month, type);
        System.out.println(result);
    }

    // 定义方法 形参: 原价 月份 类型(头等舱或者经济舱) 返回值类型 double
    public static double calcPrice(double price, int month, String type) {
        // 判断月份是淡季还是旺季
        if (month >= 5 && month <= 10) {
            switch (type) {
                case "eco":
                    price *= 0.85;
                    break;
                case "prime":
                    price *= 0.9;
                    break;
                default:
                    System.out.println("wrong input");
                    price = -1;
            }
        } else if (month == 11 || month == 12 || month >= 1 && month <= 4) {
            switch (type) {
                case "eco":
                    price *= 0.65;
                    break;
                case "prime":
                    price *= 0.7;
                    break;
                default:
                    System.out.println("wrong input");
                    price = -1;
            }
        } else {
            System.out.println("month not valid!");
        }

        return price;
    }
}

案例2. 找素数
在这里插入图片描述

package practice;

public class FindPrimeNumber {
    public static void main(String[] args) {
        // 找到101-200之间的所有质数
        for (int i = 101; i < 201; i++) {

            // 信号位
            boolean flag = true; // 假设是质数

            // 如果2-这个数的一半都没有能整除在这个数的数字 则这个数是 质数
            for (int j = 2; j < i / 2; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }

            if (flag) {
                System.out.print(i + "\t");
            }
        }
    }
}

在这里插入图片描述

案例3. 开发验证码
在这里插入图片描述

package practice;

import java.util.Random;

public class GenerateCaptcha {
    public static void main(String[] args) {
        String captcha = generateCode(10);
        System.out.println(captcha);
    }

    // n 代表验证码有几位
    public static String generateCode(int n) {
        String code = "";
        Random r = new Random();
        for (int i = 0; i < n; i++) {

            // 生成随机字符 英文大写 小写 数字 (0 1 2)
            int type = r.nextInt(3); // (0 1 2)
            switch (type) {
                case 0:
                    // 英文大写 A 65 - Z 65+25
                    char ch = (char) (r.nextInt(26) + 65);
                    code += ch;
                    break;
                case 1:
                    // 英文小写
                    char ch2 = (char) (r.nextInt(26) + 97);
                    code += ch2;
                    break;
                case 2:
                    // 数字
                    code += r.nextInt(10);
            }
        }

        return code;
    }
}

在这里插入图片描述
在这里插入图片描述

案例4. 复制数组元素
在这里插入图片描述

package practice;

public class CopyArray {
    public static void main(String[] args) {
        int[] arr1 = {11,22,33,44};

        int[] arr2 = new int[arr1.length];

        copyArr(arr1, arr2);

        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + "\t");
        }
    }

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

在这里插入图片描述

案例5. 评委打分

在这里插入图片描述

package practice;

import java.util.Scanner;

public class RateDemo {
    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) + "个评委的打分:");
            int grade = sc.nextInt();
            scores[i] = grade;
        }

        int max = scores[0], min = scores[0], sum = 0;
        for (int i = 0; i < scores.length; i++) {
            if (scores[i] > max) {
                max = scores[i];
            }

            if (scores[i] < min) {
                min = scores[i];
            }

            sum += scores[i];
        }

        double average = (sum - max - min) / (scores.length - 2);

        System.out.println("average rating is " + average);
    }
}

在这里插入图片描述

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值