判断质数Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) {
//判断200-500有多少质数
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.println("数字" + i + "是质数");
count++;
}
}
System.out.println("质数共计"+count);
}

验证码Java代码

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
    public static void main(String[] args) {
//前四位是大写字母与小写字母的随机组合,最后一位是随机数字
//先定义一个字符数组
char[] chars = new char[52];
for (int i = 0; i < chars.length; i++) {
if (i < 26) {
chars[i] = (char) ('a' + i);
} else {
chars[i] = (char) ('A' + i - 26);
}
// System.out.print(chars[i]);
}
// for (int i = 0; i < chars.length; i++) {
// System.out.print(chars[i] + " ");
// }
String result = "";
Random random = new Random();
for (int i = 0; i < 4; i++) {
//拿到四次随机的字符
int j = random.nextInt(chars.length);
result = result + chars[j];
}
//随机生成0-9的数字
int number = random.nextInt(10);
System.out.print(result + number);
}

飞机买票Java

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
 public static void main(String[] args) {
/*机票按照淡旺季进行收费 输入原价 月份 和头等舱或者经济舱进行判断
旺季(5-9)头等舱九折 经济舱8.5折,淡季 10月到来年的四月头等舱7折 经济舱6.5折
*/
Scanner sc = new Scanner(System.in);
System.out.println("请输入票价:");
int ticket = sc.nextInt();
System.out.println("请输入月份:");
int month = sc.nextInt();
System.out.println("请输入舱位 0表示头等舱 1表示经济舱");
int seat = sc.nextInt();
if (month >= 5 && month <= 9) {
// ticket = getTicket(seat, ticket, 0.9, 0.85);
ticket = getTicket(seat, ticket, 0.9, 0.85);
} else if ((month >= 1 && month <= 4) || (month >= 10 && month <= 12)) {
ticket = getTicket(seat, ticket, 0.7, 0.65);
// ticket = getTicket(seat, ticket, 0.7, 0.65);
} else {
System.out.println("输入不合法!!");
}
System.out.println("打完折的票价为:" + ticket);
}

public static int getTicket(int seat, int ticket, double x, double x1) {
if (seat == 0) {
ticket = (int) (ticket * x);
} else {
ticket = (int) (ticket * x1);
}
return ticket;
}

数组复制java

1
2
3
4
5
6
7
8
9
10
11
12
public static void main(String[] args) {
int[] arr = {1, 3, 2, 4, 5, 6, 3, 1, 13, 3, 4, 55, 6, 8};
int[] newArr = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
//i是索引值 arr[i]是值
newArr[i] = arr[i];
}

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

评委打分

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
public static void main(String[] args) {
//去掉一个最高分 去掉一个最低分 求其平均数共计六个评委分数在0-100之间

int[] scores = getScore();
int max = getMax(scores);
int min = getMin(scores);
int avg = (getSum(scores) - max - min) / (scores.length - 2);
System.out.println("该选手的最终成绩:"+avg);
}

public static int[] getScore() {
int[] arr = new int[6];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arr.length; ) {
System.out.println("请输入六组成绩:");
int score = scanner.nextInt();
if (score >= 0 && score <= 100) {
arr[i] = score;
i++;
} else {
System.out.println("成绩录入不合法,请重新录入:当前的i为"+i);
}
}
return arr;
}

//寻找最大值
public static int getMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}

//寻找最小值
public static int getMin(int[] arr) {
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}

//求数组元素之和
public static int getSum(int[] arr) {
int sum = 0;
for (int j : arr) {
sum = sum + j;
}
return sum;
}

数字加解密

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
public static void main(String[] args) {
//对数字进行加密解密操作,位数加5 在对10取余 然后 翻转
int[] arr = {1, 9, 8, 3};
//进行加5操作
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] + 5;
// System.out.print(arr[i] + " ");
}
//对10取余数操作
for (int i = 0; i < arr.length; i++) {
arr[i] = 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] + " ");
// }
//对数字进行拼接
int number=0;
for (int i = 0; i < arr.length; i++) {
number=number*10+arr[i];
}
System.out.println(number);
}
===================================================
public static void main(String[] args) {
//请输入需要解密的数字
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要解密的数字:");
int number = sc.nextInt();
int temp = number;
//计算数组的大小
int count = 0;
while (number != 0) {
number = number / 10;
count++;
}
int[] arr = new int[count];
int index = arr.length-1;
while (temp != 0) {
int ge = temp % 10;
arr[index] = ge;
index--;
temp = temp / 10;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
=================================================
public static void main(String[] args) {
//解密操作先翻转 后加五 对10反向取余
int[] arr = {8, 3, 4, 6};
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
//由于加密加五对10取余 故0-4+10,5-9不变;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 0 && arr[i] <= 4) {
arr[i] += 10;
}
}
for (int i = 0; i < arr.length; i++) {
arr[i] -= 5;
}
int number = 0;
for (int i = 0; i < arr.length; i++) {
number = number * 10 + arr[i];
}
System.out.println("解密后的数字:" + number);
}

抽奖

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
public static void main(String[] args) {
int[] arr = {1000, 23, 8, 54, 32, 12, 56, 7};
int[] arr1 = new int[arr.length];
//抽奖
Random r = new Random();
for (int i = 0; i < arr.length; ) {
//获取随机索引
int index = r.nextInt(arr.length);
int key = arr[index];
boolean flag = getComment(arr1, key);
if (!flag) {
//将奖项添加到新数组中
arr1[i] = key;
i++;
}
}
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
}

}

//判断key在数组中是否存在
public static boolean getComment(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return true;
}
}
return false;
}

抽奖优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void main(String[] args) {
//抽奖优化 打乱数组在输出
Random r = new Random();
int[] arr = {1000, 23, 8, 54, 32, 12, 56, 7};
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;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}

双色球系统java

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//6个红球和一个蓝色球 红球的号码为1-33 篮球的1-16  且红色不允重复
public static void main(String[] args) {
//1 创建出中奖号码
int[] arr = createNumber();
System.out.println("====================");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
//2 用户输入中奖号码
int[] userArr = userNumber();
for (int i = 0; i < userArr.length; i++) {
System.out.print(userArr[i]+" ");
}
//3 判断用户中奖情况
//红球中奖情况
int redCount = 0;
int blueConut = 0;
for (int i = 0; i < userArr.length - 1; i++) {
//减一判断的是蓝球
int redNumber = userArr[i];
for (int j = 0; j < arr.length - 1; j++) {
if (redNumber == arr[j]) {
redCount++;
break;
}
}
}
//判断蓝球
int blueNumber = userArr[userArr.length - 1];
if (blueNumber == arr[arr.length - 1]) {
blueConut++;
}
System.out.println("蓝球中奖个数:" + blueConut + " " + "红球中奖个数: " + redCount);

//根据红蓝球个数判断中奖情况
if (redCount == 6 && blueConut == 1) {
System.out.println("恭喜你中奖一千万!!");
}
else if (redCount == 6 && blueConut == 0) {
System.out.println("恭喜你中奖500万!!");
}
else if ((redCount == 4 && blueConut == 1) || (redCount == 5 && blueConut == 0)) {
System.out.println("恭喜你中奖2万!!");
}
else if (redCount == 5 && blueConut == 1) {
System.out.println("恭喜你中奖200万!!");
}
else if ((redCount == 2 && blueConut == 1)
|| (redCount == 2 && blueConut == 0)
|| (redCount == 1 && blueConut == 1)
){
System.out.println("恭喜你中奖10块!!");
}
else {
System.out.println("谢谢惠顾!!");
}
}

public static int[] createNumber() {
int[] arr = new int[7];
Random r = new Random();
//首先生成红球中奖号码
for (int i = 0; i < arr.length - 1; ) {
int redNumber = r.nextInt(33) + 1;
boolean falg = contains(arr, redNumber);
if (!falg) {
arr[i] = redNumber;
i++;
}
}
int blueNumber = r.nextInt(16) + 1;
arr[arr.length - 1] = blueNumber;
return arr;
}

//判断红色小球数字是否重复
public static boolean contains(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return true;
}
}
return false;
}

//用户输入号码
public static int[] userNumber() {
int[] arr = new int[7];
Scanner sc = new Scanner(System.in);
//首先生成红球中奖号码
for (int i = 0; i < arr.length - 1; ) {
System.out.print("请输入第" + (i + 1) + "个号码:");
int redNumber = sc.nextInt();
if (redNumber >= 1 && redNumber <= 33) {
//判断录入号码是否重复
boolean flag = contains(arr, redNumber);
if (!flag) {
arr[i] = redNumber;
i++;
} else {
System.out.println("当前录入号码已经存在!请重新录入:");
}
} else {
System.out.println("录入超出范围(1-33),请重新录入!");
}
}
//输入蓝球号码
System.out.println("请输入蓝球号码:");
while (true) {
int blueNumber = sc.nextInt();
if (blueNumber >= 1 && blueNumber <= 16) {
arr[arr.length - 1] = blueNumber;
break;
}
else{
System.out.println("当前蓝球录入超出范围,请重新录入:");
}
}
return arr;
}