数字加密

这里写了多个输出语句是为了出问题可以更清楚知道是哪个方法出了问题

package test;

import java.util.Scanner;

public class itheima_p75_1 {
    public static void main(String[] args) {
        System.out.print("请输入你想加密的数字:");
        Scanner r = new Scanner(System.in);
        int number = r.nextInt();
        int arr1[] = getNumber(number);
        arr1 = getPlus(arr1);
        System.out.print("\n加法处理�?: ");
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + " ");
        }
        System.out.print("\n取余处理�?: ");
        arr1 = getYu(arr1);
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + " ");
        }
        System.out.print("\n置换结果�?: ");
        arr1 = getReverse(arr1);
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + " ");
        }
        System.out.print("\n加密结果�?: "+returnNumber(arr1));
    }

    //将键盘录入的整数添加到数组当�?
    public static int[] getNumber(int number) {
        int count = 0;//计数器变�?
        int num1 = number;//记录最初number�?
        while (number != 0) {
            number = number / 10;
            count++;
        }
        int arr[] = new int[count];
        int index = arr.length - 1;
        while (num1 != 0) {
            int ge = num1 % 10;//例如1234�?10取余�?4,则ge=4
            num1 = num1 / 10;
            arr[index] = ge;
            index--;
        }
        return arr;
    }
    //对数组做+5处理
    public static int[] getPlus(int arr[]) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] + 5;
        }
        return arr;
    }
   // 对数组做�?10的余数的处理
    public static int[] getYu(int arr[]) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] % 10;
        }
        return arr;
    }
    //位置置换处理
    public static int[] getReverse(int arr[]) {
        for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
        return arr;
    }
    //将数组转化为整数
    public static int returnNumber(int arr[]){
        int num2 = 0;
        for (int i = 0; i < arr.length; i++) {
            num2 = num2 * 10 +arr[i];
        }
        return num2;
    }
}

数字解密

这里解密就是把加密的结果反过来推,即此时需要解密的数字�?8346

如图对于5-9因为�?10取余是他本身,所以解密时也是他本身;�?10-14在加密时只会留下个位数,所以在解密时,需要在他本身基础�?+10即可

package test;

import java.util.Scanner;

/*
解密程序
 */
public class itheima_p75_2 {
    public static void main(String[] args) {
        System.out.println("请输入你想解密的数字:");
        Scanner r = new Scanner(System.in);
        int number = r.nextInt();
        int arr[] = getNumber(number);
        arr = getReverse(arr);
        arr = getPlus(arr);
        arr = getReduce(arr);
/*        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }*/
        int num = returnNumber(arr);
        System.out.print("\n解密结果�?:" + num);
    }

    //将键盘录入的数字转换为数组并返回
    public static int[] getNumber(int number) {
        int count = 0;//计数器变�?
        int num1 = number;//记录最初number�?
        while (number != 0) {
            number = number / 10;
            count++;
        }
        int arr[] = new int[count];
        int index = arr.length - 1;
        while (num1 != 0) {
            int ge = num1 % 10;//例如1234�?10取余�?4,则ge=4
            num1 = num1 / 10;
            arr[index] = ge;
            index--;
        }
        return arr;
    }

    //反转数组中数�?
    public static int[] getReverse(int arr[]) {
        for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
        return arr;
    }

    //5-9之间不需要处理,0-4之间+10即可
    public static int[] getPlus(int arr[]) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= 0 && arr[i] <= 4) {
                arr[i] = arr[i] + 10;
            }
        }
        return arr;
    }

    //  �?-5处理
    public static int[] getReduce(int arr[]) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] - 5;
        }
        return arr;
    }

    //将数组转化为整数
    public static int returnNumber(int arr[]) {
        int num2 = 0;
        for (int i = 0; i < arr.length; i++) {
            num2 = num2 * 10 + arr[i];
        }
        return num2;
    }
}

抽奖

第一种算�?

这道题的核心思想是随机生成的索引可能会重复,所以需要写一个方法来进行判断

同时关键需要思路清晰,明白每一步到底需要干什么以及个变量的关�?

package test;
/*
抽奖游戏
 */
import java.util.Random;

public class itheima_p75_3 {
    public static void main(String[] args) {
        int arr[] = {2, 588, 888, 1000, 10000};
        //定义一个新数组用来存储已经获取的奖�?
        int newArr[] = new int[arr.length];
        Random r = new Random();
        for (int i = 0; i < 5; ) {
            //随机生成索引
            int index = r.nextInt(arr.length);
            //定义prize为奖励的数组中的随机数字
            int prize = arr[index];
            boolean flag = getJudge(newArr, prize);
            //判断:如果flag为true,则说明该将抽取过,无效,不做处理;
            //如果flag为false,则说明该奖项没被抽取过,有效,将该奖项加到newArr数组中,同时索引+1;
            if (!flag) {
                newArr[i] = prize;
                i++;
            }
        }
        //遍历数组
        for (int i = 0; i < newArr.length; i++) {
            System.out.println(newArr[i]);
        }
    }

    //判断生成的索引是否有�?
    public static boolean getJudge(int arr[], int prize) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == prize) {
                return true;//如果等于这个奖项,那么说明该奖项已经在该数�?,该奖项无效,返回true;
            }
        }
        return false;//该生成的索引合法,返回false
    }
}

第二种算�?

相较于第一种方法,这种方法更简单更好理解,直接将奖池的顺序进行打乱即可;与上方算法对比,在奖池较多的情况下,效率更高�?

package test;

import java.util.Random;

public class itheima_p75_4 {
    public static void main(String[] args) {
        int arr[] = {2, 588, 888, 1000, 10000};
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            int index = r.nextInt(arr.length);
            int tmp = arr[i];
            arr[i] = arr[index];
            arr[index] = tmp;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

双色球系�?

这个相对与前三个代码比较长,大家需要去理清思路再着手去�?

package test;

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

public class itheima_p75_5 {
    public static void main(String[] args) {
        int arr[] = creatNumber();//系统生成的奖池中号码
        System.out.println("系统生成的中奖号码为:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        int userInputArr[] = userInput();//用户输入的中奖号�?
        System.out.println("用户输入的号码为:");
        for (int i = 0; i < userInputArr.length; i++) {
            System.out.print(userInputArr[i] + " ");
        }
        System.out.print("\n中奖的红色球�?" + getRedNumber(arr, userInputArr) + "�?");
        System.out.print("\n中奖的蓝色球�?" + getBlueNumber(arr, userInputArr) + "�?");
        int blueNumber = getBlueNumber(arr, userInputArr);
        int redNumber = getRedNumber(arr, userInputArr);
        if (redNumber == 6 && blueNumber == 1) {
            System.out.println("\n一等奖");
        } else if (redNumber == 6 && blueNumber == 0) {
            System.out.println("\n二等�?");
        } else if (redNumber == 5 && blueNumber == 1) {
            System.out.println("\n三等�?");
        } else if (redNumber == 5 && blueNumber == 0 || redNumber == 4 && blueNumber == 1) {
            System.out.println("\n四等�?");
        } else if (redNumber == 4 && blueNumber == 0 || redNumber == 3 && blueNumber == 1 || redNumber == 2 && blueNumber == 1) {
            System.out.println("\n五等�?");
        } else if(redNumber == 1 && blueNumber == 1 || redNumber == 0 && blueNumber == 1){
            System.out.println("\n六等�?");
        }
    }
    //用户录入用户号码
    public static int[] userInput() {
        int arr[] = new int[7];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 6; ) {
            System.out.println("请输入第" + (i + 1) + "个红球数�?");
            int redNumber = sc.nextInt();
            if (redNumber >= 1 && redNumber <= 33) {
                boolean flag = getJudge(arr, redNumber);
                if (!flag) {
                    arr[i] = redNumber;
                    i++;
                } else {
                    System.out.println("您输入的号码已经存在,请重新输入");
                }
            } else {
                System.out.println("您输入的号码超出范围,请重新输入");
            }
        }
        System.out.println("请输入您的篮球号�?:");
        int blueNumber = sc.nextInt();
        while (true) {
            if (blueNumber >= 1 && blueNumber <= 16) {
                arr[arr.length - 1] = blueNumber;
                break;
            } else {
                System.out.println("您输入的篮球号码超出范围,请重新输入:");
            }
        }
        return arr;
    }

    //生成6个红球号码和1个篮球号�?
    public static int[] creatNumber() {
        int arr[] = new int[7];

        //随机生成6个红�?
        Random r = new Random();
        for (int i = 0; i < 6; ) {
            int redNumber = r.nextInt(33) + 1;
            boolean flag = getJudge(arr, redNumber);
            //判断是否重复,如果不重复则加到数组中
            if (!flag) {
                arr[i] = redNumber;
                i++;
            }
        }
        //随机生成一个蓝色球
        int blueNumber = r.nextInt(16) + 1;
        arr[arr.length - 1] = blueNumber;
        return arr;
    }

    //判断生成的索引是否重�?
    public static boolean getJudge(int arr[], int redNumber) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == redNumber) {
                return true;
            }
        }
        return false;
    }
    //获取用户号码红色球中有几个与奖池中红色球号码相同
    public static int getRedNumber(int arr[], int userInput[]) {
        int redCount = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            int redNumber = userInput[i];
            for (int j = 0; j < arr.length - 1; j++) {
                if (redNumber == arr[j]) {
                    redCount++;
                    break;
                }
            }
        }
        return redCount;
    }
    //获取用户号码蓝色球中有几个与奖池中蓝色球号码相同
    public static int getBlueNumber(int arr[], int userInput[]) {
        int blueCount = 0;
        int bluNumber = userInput[userInput.length - 1];
        if (bluNumber == arr[arr.length - 1]) {
            blueCount++;
        }
        return blueCount;
    }
}