16.2 验证码
public static String createCode(int n){
String code = "";
for (int i = 0; i < n; i++) {
Random random = new Random();
int type = random.nextInt(3);
switch (type) {
case 0:
code += (char) (random.nextInt(26) + 'A');
break;
case 1:
code += (char) (random.nextInt(26) + 'a');
break;
case 2:
code += random.nextInt(10);
break;
}
}
return code;
}
16.3 数字加密
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
System.out.println("请输入需要加密的数字个数:");
int len = cin.nextInt();
int[] arr = new int[len];
for (int i = 0; i < len; i++) {
System.out.print("请输入第"+(i+1)+"个数字:");
arr[i] = cin.nextInt();
}
encryption(arr);
printArray(arr);
}
public static void printArray(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("]");
}
public static void encryption(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = (arr[i] + 5) % 10;
}
reverse(arr);
}
public static void reverse(int[] arr) {
for (int i = 0, j = arr.length-1; i < j; i++,j--) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-n0FBzWwx-1651073702058)(image/1651059152274.png)]
16.4 模拟双色球
- 业务分析、随机生成一组中奖号码
- 用户输入一组双色球号码
- 判断中奖情况
实现
import java.util.Random;
import java.util.Scanner;
public class Test04 {
public static void main(String[] args) {
int[] luckNumbers = createLucNumber();
int [] userNumbers = userInputNumber();
judge(luckNumbers, userNumbers);
}
public static void judge(int[] luckNumbers, int[] userNumbers){
int redHitNumbers = 0;
int blueHitNumbers = 0;
for (int i = 0; i < userNumbers.length-1; i++) {
for(int j = 0; j < luckNumbers.length-1; j++){
if (userNumbers[i] == luckNumbers[j]) {
redHitNumbers++;
break;
}
}
}
int n = userNumbers.length;
blueHitNumbers = userNumbers[n-1] == luckNumbers[n-1] ? 1 : 0;
int money = 0;
if(blueHitNumbers == 1){
switch (redHitNumbers) {
case 0:
case 1:
case 2:
money = 5;
break;
case 3:
money = 10;
break;
case 4:
money = 200;
break;
case 5:
money = 3000;
break;
case 6:
money = 10000000;
break;
}
} else {
switch (redHitNumbers) {
case 4:
money = 10;
break;
case 5:
money = 200;
break;
case 6:
money = 5000000;
break;
}
}
System.out.print("开奖号码是:");
printArray(luckNumbers);
System.out.print("您的投注号码是:");
printArray(userNumbers);
System.out.println("您命中了几个红球:"+redHitNumbers);
System.out.println("您是否命中蓝球:"+(blueHitNumbers == 1 ? "是" : "否"));
if(money == 0){
System.out.println("感谢您为福利事业做出突出贡献!");
} else {
System.out.println("恭喜您,中了"+money+"元");
}
}
public static void printArray(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("]");
}
public static int[] userInputNumber(){
int[] userNumbers = new int[7];
Scanner cin = new Scanner(System.in);
for (int i = 0; i < userNumbers.length - 1; i++) {
System.out.print("请输入第"+(i+1)+"个红球号码(1-33、不重复):");
userNumbers[i] = cin.nextInt();
}
System.out.print("请输入蓝球号码(1-16、不重复):");
userNumbers[userNumbers.length-1] = cin.nextInt();
return userNumbers;
}
public static int[] createLucNumber(){
int[] numbers = new int[7];
Random random = new Random();
for (int i = 0; i < numbers.length-1; i++) {
while(true){
int num = random.nextInt(33) + 1;
if (!check(numbers, i+1, num)) {
numbers[i] = num;
break;
}
}
}
while(true){
int num = random.nextInt(16) + 1;
if (!check(numbers, numbers.length-1, num)) {
numbers[numbers.length-1] = num;
break;
}
}
return numbers;
}
public static boolean check(int[] numbers, int n, int num){
for(int i = 0; i < n; i++){
if(numbers[i] == num) {
return true;
}
}
return false;
}
}
|