Java-黑马Java学习作业-day07综合练习

学习视频链接:https://www.bilibili.com/video/BV17F411T7Ao

文章目录

  • 练习一:飞机票
  • 练习二:打印素数
  • 练习三:验证码
  • 练习四:复制数组
  • 练习五:评委打分
  • 练习六:数字加密
  • 练习七:数字解密
  • 练习八:抽奖
    • 解法一:
    • 解法二:
  • 练习九:双色球


练习一:飞机票

需求:

​ 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱。

​ 按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折。

代码示例:

import java.util.Scanner;

/**
 * @ClassName: codePractice01
 * @Author: Kox
 * @Data: 2023/1/13
 * @Sketch:
 */
public class codePractice01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入飞机原价格:");
        int price = sc.nextInt();
        System.out.print("请输入月份:");
        int month = sc.nextInt();
        System.out.print("请输入舱位:(0是头等,1是经济)");
        int level = sc.nextInt();
        if (month >= 5 && month <= 10) {
            price = getPrice(price, level, 0.9, 0.85);
        } else if ((month >= 1 && month <= 4) || (month >= 11 && month <= 12)) {
            price = getPrice(price, level, 0.7, 0.65);
        } else {
            System.out.println("月份输入错误");
        }
        System.out.println(price);
    }

    public static int getPrice(int price, int level , double v0, double v1) {
        if (level == 0) {
            price = (int) (price * v0);
        } else if (level == 1){
            price = (int) (price * v1);
        } else {
            System.out.println("没有这个舱位!");
        }
        return price;
    }
}

练习二:打印素数

​ 判断101~200之间有多少个素数,并输出所有素数。

备注:素数就是质数

代码示例:

/**
 * @ClassName: cn.kox.practice.codePractice02
 * @Author: Kox
 * @Data: 2023/1/13
 * @Sketch: 找质数
 */
public class codePractice02 {
    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; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                System.out.print(i + "\t");
                count++;
            }
        }
        System.out.println("\n有" + count + "个质数。");
    }
}

练习三:验证码

需求:

​ 定义方法实现随机产生一个5位的验证码

验证码格式:

​ 长度为5

​ 前四位是大写字母或者小写字母

​ 最后一位是数字

代码示例:

import java.util.Arrays;
import java.util.Random;

/**
 * @ClassName: codePractice03
 * @Author: Kox
 * @Data: 2023/1/13
 * @Sketch: 开发验证码
 */
public class codePractice03 {
    public static void main(String[] args) {
        char[] letter = new char[52];
        for (int i = 0; i < letter.length; i++) {
            if (i <= 25) {
                letter[i] = (char)(97 + i);
            } else {
                letter[i] = (char)(65 + i - 26);
            }
        }
        Random r = new Random();
//        String verify = "";
        char[] verify = new char[5];
        for (int i = 0; i <= 4; i++) {
            int index = r.nextInt(letter.length - 1) + 1;
            if (include(verify, letter, index)) {
                verify[i] = letter[index];
            }
            if (i == 4) {
                index = r.nextInt(10) + 48;
                verify[i] = (char) index;
            }
        }
        System.out.println(Arrays.toString(verify));
    }

    public static boolean include(char[] brr, char[] arr, int num) {
        for (int i = 0; i < brr.length - 1; i++) {
            if (brr[i] == arr[num]) {
                return false;
            }
        }
        return true;
    }
}

练习四:复制数组

需求:

​ 把一个数组中的元素复制到另一个新数组中去。

代码示例:

import java.util.Arrays;

/**
 * @ClassName: codePractice04
 * @Author: Kox
 * @Data: 2023/1/13
 * @Sketch: 数组元素复制
 */
public class codePractice04 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 5, 6, 99, 100};
        int[] brr = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            brr[i] = arr[i];
        }
        System.out.println(Arrays.toString(brr));
    }
}

练习五:评委打分

需求 :

​ 在唱歌比赛中,有6名评委给选手打分,分数范围是[0 - 100]之间的整数。选手的最后得分为:去掉最高分、最低分后的4个评委的平均分,请完成上述过程并计算出选手的得分。

代码示例:

import java.util.Arrays;
import java.util.Random;

/**
 * @ClassName: codePractice05
 * @Author: Kox
 * @Data: 2023/1/13
 * @Sketch: 评委打分
 */
public class codePractice05 {
    public static void main(String[] args) {
        int[] arr = new int[6];     // 评委的分数
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            int rScore = r.nextInt(101);
            arr[i] = rScore;
        }
        int score = 0;
        for (int i = 0; i < arr.length; i++) {
            score += arr[i];
        }
        int maxScore = getMax(arr);
        int minScore = getMin(arr);
        double endScore = (double) (score - maxScore - minScore) / (arr.length - 2);
        System.out.println(Arrays.toString(arr));
        System.out.println("去掉一个最高分:" + maxScore + ",去掉一个最低分:" + minScore + ",最后得分" + endScore + "分儿。");
    }

    public static int getMax(int[] arr) {
        int maxScore = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (maxScore < arr[i]) {
                maxScore = arr[i];
            }
        }
        return maxScore;
    }

    public static int getMin(int[] arr) {
        int minScore = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (minScore > arr[i]) {
                minScore = arr[i];
            }
        }
        return minScore;
    }
}

练习六:数字加密

需求:

​ 某系统的数字密码(大于0),比如1983,采用加密方式进行传输。

规则如下:

​ 先得到每位数,然后每位数都加上5 , 再对10求余,最后将所有数字反转,得到一串新数。

举例:

		1	9	8	3
+5		6   14  13  8
%10		6   4   3   8
反转	   8   3   4   6
加密后的结果就是:8346

代码示例:

/**
 * @ClassName: codePractice06
 * @Author: Kox
 * @Data: 2023/1/14
 * @Sketch: 数字加密
 */
public class codePractice06 {
    public static void main(String[] args) {
        int[] arr = {1, 9 , 8 , 3};
        int number = 0;
        for (int i = arr.length-1; i >= 0; i--) {
            number = number * 10 + getNum(arr[i]);
        }
        System.out.print("加密后的结果就是:" + number);
    }

    public static int getNum(int num) {
        num = (num + 5) % 10;
        return num;
    }

}

练习七:数字解密

把上一题加密之后的数据进行解密

代码示例:

/**
 * @ClassName: codePractice07
 * @Author: Kox
 * @Data: 2023/1/14
 * @Sketch: 数字解密
 */
public class codePractice07 {
    public static void main(String[] args) {
        int[] arr = {8, 3, 4, 6};
        int number = 0;
        for (int i = arr.length-1; i >= 0; i--) {
            number = number * 10 + getNum(arr[i]);
        }
        System.out.println(number);
    }

    public static int getNum(int num) {
        num = (num + 5) % 10;
        return num;
    }
}

练习八:抽奖

需求:

​ 一个大V直播抽奖,奖品是现金红包,分别有{2, 588 , 888, 1000, 10000}五个奖金。请使用代码模拟抽奖,打印出每个奖项,奖项的出现顺序要随机且不重复。打印效果如下:(随机顺序,不一定是下面的顺序)

888元的奖金被抽出
588元的奖金被抽出
10000元的奖金被抽出
1000元的奖金被抽出
2元的奖金被抽出

解法一:

import java.util.Arrays;
import java.util.Random;

/**
 * @ClassName: codePractice08
 * @Author: Kox
 * @Data: 2023/1/14
 * @Sketch: 抽奖
 */
public class codePractice08 {
    public static void main(String[] args) {
        int[] arr = {2, 588, 1000, 10000};
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            int index = r.nextInt(arr.length);
            int temp = arr[i];
            arr[i] = arr[index];
            arr[index] =temp;
        }
        System.out.println(Arrays.toString(arr));
    }

}

解法二:

import java.util.Random;

/**
 * @ClassName: codeHomework11
 * @Author: Kox
 * @Data: 2023/1/10
 * @Sketch: 题目11
 */
public class codeHomework11 {
    public static void main(String[] args) {
        int[] arr = {2, 588, 1000, 10000};
        int[] brr = new int[arr.length];
        Random r = new Random();
        int flag = 0;
        while (flag < arr.length) {
            int index = r.nextInt(arr.length);
            if (returnNum(brr, arr[index])) {
                brr[flag] = arr[index];
                System.out.println(brr[flag] + "元的奖金被抽出");
                flag++;
            }
        }
    }

    public static boolean returnNum(int[] arr, int num) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num) {
                return false;
            }
        }
        return true;
    }
}

练习九:双色球

Java-黑马Java学习作业-day07综合练习_第1张图片
代码示例:

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

/**
 * @ClassName: codePractice09
 * @Author: Kox
 * @Data: 2023/1/14
 * @Sketch: 双色球系统
 */
public class codePractice09 {
    public static void main(String[] args) {
        int[] red = new int[33];
        red = getArr(red);

        int[] blue = new int[16];
        blue = getArr(blue);

        int[] luckyArr = new int[7];    // 抽出的中奖数组
        Random r = new Random();
        int index = 0;
        while (index < luckyArr.length) {
            int redIndex = r.nextInt(red.length);
            if (getNum(luckyArr, red[redIndex])) {
                luckyArr[index] = red[redIndex];
                index++;
            }
            if (index == luckyArr.length - 1) {
                int blueIndex = r.nextInt(blue.length);
                luckyArr[index] = blue[blueIndex];
                index++;
            }
        }

        // System.out.println("本次奖池号码为:" + Arrays.toString(luckyArr));

        int[] userArr = new int[luckyArr.length];   // 用户输入的号码
        Scanner sc = new Scanner(System.in);
        int count = 1;
        while (count <= userArr.length) {
            if (count == userArr.length) {
                System.out.print("输入蓝色球的号码:");
                int num = sc.nextInt();
                if (num >= blue[0] && num <= blue[blue.length-1]) {
                    userArr[count-1] = num;
                    count++;
                } else {
                    System.out.println("奖池没有该数字!");
                }
            } else {
                System.out.print("输入红色球的第" + count + "个数字:");
                int num = sc.nextInt();
                if (num >= red[0] && num <= red[red.length-1]) {
                    if (getNum(userArr,num)) {
                        userArr[count-1] = num;
                        count++;
                    } else {
                        System.out.println("数字输入重复!");
                    }
                } else {
                    System.out.println("奖池没有该数字!");
                }
            }
        }
        int redCount = 0;
        int blueCount = 0;
        for (int i = 0; i < userArr.length; i++) {
            if (i == userArr.length-1) {
                if (luckyArr[i] == userArr[i]) {
                    blueCount++;
                }
            } else {
                if (luckyArr[i] == userArr[i]) {
                    redCount++;
                }
            }
        }
        System.out.println("本次奖池号码为:" + Arrays.toString(luckyArr));
        System.out.println("你竞猜的号码为:" + Arrays.toString(userArr));
        System.out.println("红蓝中将次数分别是:"+ redCount + ":" + blueCount);
        switch (redCount) {
            case 0,1,2 -> System.out.println(blueCount != 0 ? "六等奖5元" : redCount != 0 ? "六等奖:5元" : "无奖");
            case 3 -> System.out.println(blueCount != 0 ? "五等奖:10元" : "六等奖:5元");
            case 4 -> System.out.println(blueCount != 0 ? "四等奖:200元" : "五等奖:10元");
            case 5 -> System.out.println(blueCount != 0 ? "三等奖:3000元" : "四等奖:200元");
            case 6 -> System.out.println(blueCount != 0 ? "一等奖:最高1000万" : "二等奖:最高500万");
        }

    }

    public static int[] getArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i + 1;
        }
        return arr;
    }

    public static boolean getNum(int[] arr, int num) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num) {
                return false;
            }
        }
        return true;
    }


}

你可能感兴趣的:(Java,SE,java,学习,开发语言,算法)