Java学习-编程训练案例

目录

一、买飞机票

二、找素数 

三、开发验证码

四、数组元素的复制

五、评委打分

六、数字加密

七、模拟双色球


一、买飞机票

需求:
●机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱。 
●机票最终优惠价格的计算方案如下: 旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头 等舱7折,经济舱6.5折。
分析: 
●键盘录入机票的原价,仓位类型,月份信息,调用方法返回机票最终的优惠价格。 
●方法内部应该先使用if分支判断月份是是旺季还是淡季,然后使用switch分支判断是头等舱还是经济舱。 


代码如下:

package com.zwcode.test;

import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入机票原价:");
        double price = scanner.nextDouble();
        System.out.println("请输入月份:");
        int month = scanner.nextInt();
        System.out.println("请输入仓位类型(头等舱/经济舱):");
        String type = scanner.next();
        double discountPrice = discountPrice(price, month, type);
        System.out.println("最终优惠价格为:" + discountPrice);

    }

    public static double discountPrice(double price,int month,String type){
        //旺季
        if (month >= 5 && month <= 10){
            if (type.equals("头等舱")){
                price = price * 0.9;
            } else if (type.equals("经济舱")){
                price = price * 0.85;
            }else {
                System.out.println("您输入的仓位类型有误,请重新输入!");
                return -1;
            }
        }else if (month == 11 || month == 12 || (month >= 1 && month <= 4)){
            if (type.equals("头等舱")){
                price = price * 0.7;
            } else if (type.equals("经济舱")){
                price = price * 0.65;
            }else {
                System.out.println("您输入的仓位类型有误,请重新输入!");
                return -1;
            }
        }else {
            System.out.println("您输入的月份有误,请重新输入!");
            return -1;
        }
        return price;
    }
}

二、找素数 

判断101-200之间有多少素数并将它们输出

分析:用循环拿到101-200之间的数,每拿到一个数就判断是否为素数

判断规则:从2开始遍历到该数的一半,看是否有数可以整除它

package com.zwcode.test;

public class Test02 {
    public static void main(String[] args) {
        //记录素数个数
        int count = 0;
        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.println(i);
                count++;
            }
        }
        System.out.println("素数个数:" + count);
    }
}

三、开发验证码

要求:随机产生五位验证码,可能是数字大写字母或者小写字母

分析:
①定义一个方法,生成验证码返回:方法参数是位数、方法的返回值类型是String。
②在方法内部使用for循环生成指定位数的随机字符,并连接起来。
③把连接好的随机字符作为- -组验证码进行返回。
 

package com.zwcode.test;

import java.util.Random;

/*随机生成n位验证码*/
public class Test03 {
    public static void main(String[] args) {
        //想要一个随机五位验证码
        String code = creatCode(5);
        System.out.println(code);
    }

    public static String creatCode(int n){
        String code = "";
        for (int i = 0; i < n; i++) {
            Random r = new Random();
            int type = r.nextInt(3);//0大写字母、1小写字母、2数字
            switch (type){
                case 0:
                    //大写字母:A-Z(0-25)+65
                    char big = (char) (r.nextInt(26) + 65);
                    code += big;
                    break;
                case 1:
                    //小写字母:a-z(0-25)+97
                    char small = (char) (r.nextInt(26) + 97);
                    code += small;
                    break;
                case 2:
                    code += r.nextInt(10);
                    break;
            }
        }
        return code;
    }
}

四、数组元素的复制

需求:●把一个数组中的元素复制到另一个新数组中去。
分析:●需要动态初始化-一个数组,长度与原数组一样。
        ●遍历原数组的每个元素,依次赋值给新数组。
        ●输出两个数组的内容。

package com.zwcode.test;

public class Test04 {
    public static void main(String[] args) {
        int a[] = {1,2,3,4};
        int n = a.length;
        int b[] = new int[n];
        copyArr(a,b);
        printArr(a);
        printArr(b);
    }
    public static void copyArr(int arr1[],int arr2[]){
        for (int i = 0; i < arr1.length; i++) {
            arr2[i] = arr1[i];
        }
    }

    public static void printArr(int arr[]){
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");
        }
        System.out.println("]");
    }
}

五、评委打分

需求:
在唱歌比赛中,有6名评委给选手打分,分数范围是[0 - 100]之间的整数。选手的最后得分为:去掉最
高分、最低分后的4个评委的平均分,请完成上述过程并计算出选手的得分。
分析:
①把6个评委的分数录入到程序中去--->使用数组
int[] scores = new int[6];
②遍历数组中每个数据,进行累加求和,并找出最高分、最低分。
③按照分数的计算规则算出平均分。
 

package com.zwcode.test;

import java.util.Scanner;

public class Test05 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //打分人数
        System.out.println("请输入打分人数:");
        int n = sc.nextInt();
        int score[] = new int[n];
        //打分
        for (int i = 0; i < score.length; i++) {
            System.out.println("请输入第" + i + 1 + "个分数:");
            score[i] = sc.nextInt();
        }
        int average = average(score);
        System.out.println("选手最终得分为:" + average);
    }

    public static int average(int score[]){
        //最大分
        int max = score[0];
        //最小分
        int min = score[0];
        //求和
        int sum = 0;
        for (int i = 0; i < score.length; i++) {
            //得到最大值
            if (score[i] > max){
                max = score[i];
            }
            //得到最小值
            if (score[i] < min){
                min = score[i];
            }
            //得到总分
            sum += score[i];
        }

        //平均分
        return (sum - max - min)/(score.length - 2);
    }


    /**
     * 以下方法太麻烦,一个循环就可以
     * @param score
     * @return
     */
    /*public static int average(int score[]){
        for (int i = 0; i < score.length - 1; i++) {
            for (int j = 0; j < score.length - i - 1; j++) {
                if (score[j] > score[j+1]){
                    int temp;
                    temp = score[j];
                    score[j] = score[j+1];
                    score[j+1] = temp;
                }
            }
        }
        int sum = 0;
        for (int i = 1; i < score.length - 1; i++) {
            sum += score[i];
        }
        return sum / (score.length - 2);
    }*/
}

六、数字加密

需求:
●某系统的数字密码:比如1983,采用加密方式进行传输,规则如下:先得到每位数,然后每位数都加上
5,再对10求余,最后将所有数字反转,得到一串新数。

 分析
●将每位数据存入到数组中去,遍历数组每位数据按照规则进行更改,把更改后的数据从新存入到数组中。
●将数组的前后元素进行交换,数组中的最终元素就是加密后的结果。

package com.zwcode.test;

import java.util.Scanner;

public class Test06 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入密码长度:");
        int n = sc.nextInt();
        //密码数组
        int a[] = new int[n];
        //输入密码
        for (int i = 0; i < a.length; i++) {
            System.out.println("请您输入第" + (i + 1) + "位密码:");
            a[i] = sc.nextInt();
        }
        encrypt(a);
    }

    public static void encrypt(int arr[]){
        //+5  %10
        for (int i = 0; i < arr.length; i++) {
            arr[i] += 5;
            arr[i] %= 10;
        }

        //反转
        for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }
}

七、模拟双色球

本个案例比上面要难一点

首先了解双色球的组成,是由六个红色球(1-33)和一个蓝色球(1-26)组成,且红色球数字不能有重复的。

分析:共分为三步

第一步:定义数组,随机生成一个双色球号码,先随机生成六个1-33的号码(注意:不能重复),再生成一个1-16的号码

第二步:用户选号

第三步:判断是否中奖(中奖规则:蓝球中一个并且红球三个以下为5元;蓝球一个红球三个或者蓝球0个红球四个为10元;蓝球一个红球四个或者蓝球0个红球5个位200元;蓝球一个红球5个为3000元;红球6个蓝球0个最高500万;红球6个蓝球1个最高1000万)

package com.zwcode.test;

import java.util.Random;
import java.util.Scanner;

/*模拟双色球*/
public class Test07 {
    public static void main(String[] args) {
        //第一步:随机生成六个红球号码(1-33)和一个篮球号码(1-16),用一个方法来实现createLuckNumber
        int[] luckNumbers = createLuckNumbers();
        printArr(luckNumbers);
        //第二步:用户选号
        int[] inputNumbers = userInputNumbers();
        printArr(inputNumbers);
        //第三步:判断是否中奖
        judge(luckNumbers,inputNumbers);
    }

    //随机生成双色球号码
    public static int[] createLuckNumbers() {
        //定义一个数组存放七个数字
        int[] numbers = new int[7];
        //随机数
        Random r = new Random();
        //先假设没有重复的
        boolean flag = true;
        //生成六个红色球号码,但需注意的是,可能会有重复的
        for (int i = 0; i < numbers.length - 1; i++) {
            //用死循环,直到号码不重复才终止
            while (true) {
                int data = r.nextInt(33) + 1;//范围为1-33
                //判断是否重复[11,22,11],重复就推出循环,会重新赋值
                for (int j = 0; j < i; j++) {
                    if (data == numbers[j]) {
                        flag = false;
                        break;
                    }
                }
                //不重复将值付给numbers
                if (flag) {
                    numbers[i] = data;
                    break;
                }
            }
        }
        numbers[numbers.length - 1] = r.nextInt(16) + 1;
        return numbers;
    }

    //用户选号
    public static int[] userInputNumbers(){
        int[] numbers = new int[7];
        //选红色球
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < numbers.length - 1; i++) {
            System.out.println("请输入要选的第" + (i+1) + "个红球号码(范围:1-33,不要重复)");
            numbers[i] = sc.nextInt();
        }
        System.out.println("请输入要选的蓝球号码(范围:1-16)");
        numbers[numbers.length - 1] = sc.nextInt();
        return numbers;
    }


    public static void judge(int[] luckNumbers,int[] inputNumbers){
        //相等个数
        //红球
        int redHitNumbers = 0;
        //蓝球
        int blueHitNumbers = 0;
        //比较红球是否有相等的
        for (int i = 0; i < inputNumbers.length - 1; i++) {
            for (int j = 0; j < luckNumbers.length - 1; j++) {
                if (inputNumbers[i] == luckNumbers[j]){
                    redHitNumbers++;
                }
            }
        }
        //比较蓝球是否有相等的
        if (inputNumbers[inputNumbers.length - 1] == luckNumbers[luckNumbers.length - 1]){
            blueHitNumbers++;
        }
        if (blueHitNumbers == 1 && redHitNumbers < 3){
            System.out.println("恭喜你中奖5元!");
        }else if ((blueHitNumbers == 1 && redHitNumbers == 3) || (blueHitNumbers == 0 && redHitNumbers == 4)){
            System.out.println("恭喜你中奖10元!");
        }else if ((blueHitNumbers == 1 && redHitNumbers == 4) || (blueHitNumbers == 0 && redHitNumbers == 5)){
            System.out.println("恭喜你中奖200元!");
        }else if (blueHitNumbers == 1 && redHitNumbers == 5){
            System.out.println("恭喜你中奖3000元!");
        }else if (blueHitNumbers == 0 && redHitNumbers == 6){
            System.out.println("恭喜你中奖500万元!");
        }else if (blueHitNumbers == 1 && redHitNumbers == 6){
            System.out.println("恭喜你中奖1000万元!");
        }else {
            System.out.println("谢谢惠顾!");
        }
    }


    public static void printArr(int[] arr){
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");
        }
        System.out.println("]");
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值