Java案例

1,数字加密

  • 需求:某系统的数字密码是一个四位数,如1983,为了安全,需要加密后在进行传输,加密规则是:对密码中的每位数字都加5,再对10求余数,最后将所有数字顺序反转,得到一串加密后的新数字,请设计出满足本需求的加密程序。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.itheima;

import java.util.Scanner;

public class Encryption {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要加密的数字:");
int num = sc.nextInt(); // 接收用户的输入
String data = encrypt(num);
System.out.println("最终的加密字符为:" + data);
}
public static String encrypt(int num){
/*
将输入的数字进行加密
1,先将四位数字拆分
2,用数组将每一位存储起来
3,对数字进行加5对10取余
4,将数组元素反转
*/
int[] arry = new int[4];
arry[3] = num % 10; // 取个位
arry[2] = (num / 10) % 10; //取十位
arry[1] = (num / 100) % 10; // 取百位
arry[0] = num / 1000; // 取千位
for (int i = 0; i < arry.length; i++) {
// 对数组元素+5 然后对10取余
arry[i] += 5;
arry[i] = arry[i] % 10;
}
for (int i = 0; i < (arry.length / 2); i++) {
// 将第i个元素和第4-i个元素进行交换
int temp = arry[i];
arry[i] = arry[arry.length -i -1];
arry[arry.length -i -1] = temp;
}
String str = "";
String data = str + arry[0] + arry[1]+ arry[2]+ arry[3];

return data;
}
}

2,评委打分

  • 需求:键盘键入六位评委的分数,去掉最高分,去掉最低分,最终得分为四位评委评分的平均分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int[] arra = new int[6];  // 定义长度为6的数组
Scanner sc = new Scanner(System.in);
int j = 0;
for (int i = 0; i < arra.length;) {
System.out.println("请您依次输入六位评委的打分:"); // 使用键盘录入,1-100
arra[i] = sc.nextInt(); // 依次接收用户的输入
if (arra[i] >= 1 && arra[i] <=100){
i++;
}
else {
System.out.println("不合法的值!请您重新输入。");
}
}

// 求最大值和最小值
int max = arra[0];
for (int i = 0; i < arra.length; i++) {
if (arra[i] > max){
max = arra[i];
}
}
int min = arra[0];
for (int i = 0; i < arra.length; i++) {
if (arra[i] < min){
min = arra[i];
}
}
int six_sum = 0;
for (int i = 0; i < arra.length; i++) {
six_sum += arra[i];
}
int avg = (six_sum - min - max) / 4;
System.out.println("最终得分为:" + avg);

3,抢红包

  • 思路:

1)首先,写一个循环,循环次数为数组的长度
2)每次循环,键盘录入,提示**”用户录入任意键抽奖:
3)随机从数组中产生一个索引,获取索引位置的元素,这个元素就表示抽的红包
如果值不为
0,则打印如:恭喜您,您抽中了520“,把这个位置元素置为0*
如果值为0,则说明这个红包被抽过,重新循环到第2步,重新抽奖
【注意:如果当前这一次没有抽中,这一次抽奖机会被浪费掉了,我们可以把控制循环的次数自减一下】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int[] moneys = {100,999,50,520,1314};
Random rd = new Random();
Scanner sc = new Scanner(System.in);
for (int i = 0; i < moneys.length; i++) {
System.out.println("请您输入任意键抽奖!");
String s = sc.next();
int index = rd.nextInt(5); // 从0-4随机产生一个数
if (index <= 4 && index >= 0) {
//说明随机产生的数在数组中
if (moneys[index] != 0) {
System.out.println("恭喜您,您抽中了" + moneys[index] + "元");
moneys[index] = 0;
} else {
System.out.println("很遗憾没有中奖!");
}
}else {
System.out.println("很遗憾没有中奖!");
}
}

4,双色球中奖

  • 思路:

    1, 用户输入一组号码(前六个红色1-33,后一个蓝色1-16
    2,系统随机产生一组中奖号码
    3,两个数组进行比对,看用户是否中奖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.itheima;

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

public class Double_Color_ball {
public static void main(String[] args) {
/*
1, 用户输入一组号码(前六个红色1-33,后一个蓝色1-16
2,系统随机产生一组中奖号码
3,两个数组进行比对,看用户是否中奖
*/
int[] arr1 = new int[7]; // 定义空数组,长度为7
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr1.length; i++) {
System.out.println("请您输入第" + (i+ 1) + "个双色球号码!(1-33)");
int b = sc.nextInt(); // 接收用户输入
if (b <=33 && b >=1){
arr1[i] = b;
}else {
System.out.println("输入有误,请您输入(1-33)之间的数字!");
i --;
}
}
System.out.println("请您输入最后一个双色球号码(1-16)!");
int a = sc.nextInt();
if (a >=1 && a <=16){
arr1[6] = a;
}else {
System.out.println("输入有误,请您输入(1-16)之间的数字!");
}
// 用户输入完毕
Random rd = new Random();
int[] arr2 = new int[7];
for (int i = 0; i < 6; i++) {
// 随机产生前六个号码
arr2[i] = rd.nextInt(34); // 产生1-33
}
arr2[6] = rd.nextInt(17); // 产生1-16
judge(arr1,arr2); // 对比两个数组

}
public static int judge(int[] arr1, int[] arr2){
/*
- 6个红球+1个蓝球 ,奖金1000万
- 6个红球+0个蓝球,奖金500万
- 5个红球+1个蓝球,奖金3000块
- 5个红球+0个蓝球,或者4个红球+1个蓝球,奖金200块
- 4个红球+0个蓝球,或者3个红球+1个蓝球,奖金10块
- 小于3个红球+1个蓝球,奖金5块
- 如果前面的都不成立,就中奖,算你为福利事业做贡献了。
*/
int bluecount = 0; // 如果最后一个元素相同bluecount++
int redcount = 0; // 如果用户数组中有一个元素和电脑数组中的元素一样redcount++
for (int i = 0; i < 6; i++) {
// 比较前六个元素
for (int j = 0; j < 6; j++) {
if(arr2[i] == arr1[j]){
redcount ++; // 有相同的元素bluecount++
}else continue; // 没有就继续比对
}
}
if (arr1[6] == arr2[6]){
redcount ++;
}
if (redcount == 6 && bluecount == 1){
System.out.println("恭喜您中奖了!奖金1000万");
}else if (redcount ==6 && bluecount == 0){
System.out.println("恭喜您中奖了!奖金500万");
}else if (redcount == 5 && bluecount ==1){
System.out.println("恭喜您中奖了!奖金3000块");
} else if ((redcount ==5 && bluecount == 0) ||(redcount == 4 && bluecount ==1)) {
System.out.println("恭喜您中奖了!奖金200块");
} else if ((redcount ==4 && bluecount == 0) ||(redcount == 3 && bluecount ==1)) {
System.out.println("恭喜您中奖了!奖金10块");
}else if (redcount <= 3 && bluecount ==1) {
System.out.println("恭喜您中奖了!奖金5块");
}else {
System.out.println("福利事业多亏了你的贡献");
}

return 0;
}
}