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){
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++) { arry[i] += 5; arry[i] = arry[i] % 10; } for (int i = 0; i < (arry.length / 2); 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]; Scanner sc = new Scanner(System.in); int j = 0; for (int i = 0; i < arra.length;) { System.out.println("请您依次输入六位评委的打分:"); 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); 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 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) {
int[] arr1 = new int[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); } arr2[6] = rd.nextInt(17); judge(arr1,arr2);
} public static int judge(int[] arr1, int[] arr2){
int bluecount = 0; int redcount = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if(arr2[i] == arr1[j]){ redcount ++; }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; } }
|