当前位置: 首页 > news >正文

caseExercise

随机验证码

package com.fan.caseExercise;import java.util.Random;public class Demo01 {public static void main(String[] args) {String code = createCode(5); //输入你想要的验证码位数System.out.println(code);}public static String createCode(int n){//1.定义一个for循环,用于控制产生多少位随机字符Random random = new Random();//3.定义一个String类型的变量,用于记忆产生的每位随机字符String code = "";for (int i = 1; i <= n; i++) {//2.为每一个位置生成一个随机字符int r = random.nextInt(3); //随机一个 0 1 2 ,0代表随机数字;1 2代表随机大小写字母switch (r){case 0://随即一个数字code += random.nextInt(10);break;case 1://随机一个大写字母 A 65   Z 65+25   (0-25)+ 65char ch1 = (char) (random.nextInt(26) + 65);code += ch1;break;case 2://随机一个小写字母 a 97   z 97+25char ch2 = (char) (random.nextInt(26) + 97);code += ch2;break;}}return code;}
}

评委打分

package com.fan.caseExercise;import java.util.Scanner;public class Democracy {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入评委的人数:");int count = scanner.nextInt();//引用方法 (直接输出引用)System.out.println(arvergeScore(count));}public static double arvergeScore (int num) {//接收评委打分int[] score = new int[num];for (int i = 0; i < score.length; i++) {System.out.println("请输入第" + (i+1) + "个评委的打分:");Scanner scanner1 = new Scanner(System.in);int score1 = scanner1.nextInt();score[i] = score1;}//冒泡排序 (升序)int x =0;for (int i = 0; i < score.length; i++) {for (int j = 0; j < score.length - 1; j++) {if (score[j] > score[j+1]){x = score[j];score[j] = score[j+1];score[j+1] = x;}}}//舍去极端值,求平均数double sum = 0;for (int i = 0; i < score.length - 1; i++) {if (i != 0){sum += score[i];}}//返回结果//return sum;return 1.0 * (sum / (score.length - 2));}
}

数字加密

package com.fan.caseExercise;import java.util.Scanner;public class Demo03 {public static void main(String[] args) {int[] code = new int[4];System.out.println("请依次输入您的四位数密码:");//对Scanner使用for循环捕捉键盘上的数据并依次交给code数组for (int i = 0; i <= 3; i++) {Scanner scanneri = new Scanner(System.in);code[i] = scanneri.nextInt();}//遍历加密后的数组for (int i:enc(code)){System.out.print(i + "\t");}}public static int[] enc (int[] array) {int[] x = new int[array.length];//承接加密后的密码int j = array.length-1;//加密for (int i = 0; i < array.length; i++) {if (j>=0){x[j] = (array[i] + 5) % 10; //对code数组中的每个数字进行+5 再对10求余 最后反转j--;}}//返回加密后的密码xSystem.out.println("-------------");return x;}
}

精简优化版

package com.fan.caseExercise;import java.util.Scanner;public class Demo03 {public static void main(String[] args) {int[] code = new int[4];System.out.println("请依次输入您的四位数密码:");//对Scanner使用for循环捕捉键盘上的数据并依次交给code数组for (int i = 0; i <= 3; i++) {Scanner scanneri = new Scanner(System.in);code[i] = scanneri.nextInt();}//遍历加密后的数组for (int i:enc(code)){System.out.print(i + "\t");}}public static int[] enc (int[] array) {int[] x = new int[array.length];//承接加密后的密码//加密for (int i = 0, j = array.length-1; i < array.length && j>=0; i++,j--) {x[j] = (array[i] + 5) % 10; //对code数组中的每个数字进行+5 再对10求余 最后反转}//返回加密后的密码xSystem.out.println("-------------");return x;}
}

抽红包

1.0版本(多损耗性能)

package com.fan.caseExercise;import java.util.Random;
import java.util.Scanner;public class Demo04 {public static void main(String[] args) {//设立奖池int[] moneys = {9,666,188,520,99999};//调用抽奖方法draw(moneys);}public static void draw (int[] arr){Scanner sc = new Scanner(System.in);System.out.println("请输入任意符号开始抽奖!");//抽5次奖for (int i = 1; i <= 5; i++) {sc.next(); //Enter进行下一步操作 模拟抽奖开始按钮while (true) {Random r = new Random();int index = r.nextInt(arr.length);if (arr[index] != 0) { //若等于0表示轮空,系统内部重新抽奖System.out.println("您的中奖金额为:" + arr[index]);arr[index] = 0; //刷新奖池break; //结束本次抽奖}}}System.out.print("--------------" + "\n" + "奖池已瓜分完毕!");}
}

2.0版本(利用换牌算法实现)

package com.fan.caseExercise;import java.util.Random;
import java.util.Scanner;public class Demo05 {public static void main(String[] args) {//设立奖池int[] moneys = {9,666,188,520,99999};//调用抽奖方法draw(moneys);}public static void draw (int[] arr){//洗牌算法(换牌算法)//每次让数组中第 n=arr.length-1 (n--)元素与任意一个元素互换//(包括自己与自己互换)(互换后锁定最后一个元素)for (int i = arr.length-1; i > 0; i--) { //选取数组中最后一个元素Random r = new Random();int j = r.nextInt(i+1); //从下标(0~i)中随机选取一个元素//arr[i]与arr[j]互换int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}Scanner sc = new Scanner(System.in);System.out.println("请输入任意字符并按下Enter键开始抽奖!");for (int i = 0; i < arr.length; i++) {sc.next(); //模拟用户开始抽奖交互按钮System.out.print("您的中奖金额为:" + arr[i] + "\n-----------------\n");}System.out.println("奖池已瓜分完毕!");}
}

找质数

package com.fan.caseExercise;public class Demo06 {public static void main(String[] args) {System.out.println(prim(101,200));}public static int prim (int start,int end){int count = 0;OUT:for (int i = start; i <= end ; i++) {for (int j = 2; j <= Math.sqrt(i); j++) {if (i % j == 0){continue OUT;}}System.out.println(i);count++;}System.out.println("质数的个数为:");return count;}
}

双色球

package com.fan.caseExercise;import java.util.Random;
import java.util.Scanner;public class Demo07 {public static void main(String[] args) {int[] user = DCB();int[] lucy = creatLucyNumbers();System.out.print("您的号码为:\t");PrintArray(user);System.out.print("中奖号码为:\t");PrintArray(lucy);System.out.print("中奖情况为:\t");judge(user,lucy);}//打印数组public static void PrintArray(int[] arr) {System.out.print("[");for (int i = 0; i < 7; i++) {System.out.print(i == 6 ? arr[i] : arr[i] + ",");}System.out.println("]");}//读取用户输入的双色球号码public static int[] DCB (){int[]  dbc= new int[7];Scanner sc = new Scanner(System.in);Scanner sc1 = new Scanner(System.in);//红球for (int i = 0; i < 6; i++) {while (true) {System.out.println("请输入第"+(i+1)+"个红球号码(1~33):");int num = sc.nextInt();if (num < 1 || num > 33) {System.out.println("号码超出范围,请重新输入!");}else {if (chRe(dbc,num)){dbc[i] = num;break;}else {System.out.println("号码重复,请重新输入!");}}}}//蓝球while (true) {System.out.println("请输入蓝球号码(1~16):");int num = sc1.nextInt();if (num < 1 || num > 16) {System.out.println("号码超出范围,请重新输入!");} else {dbc[6] = num;break;}}return dbc;}//检查号码是否重复(T代表不重复)public static boolean chRe (int[] arr,int number){boolean x = true;for (int i = 0; i < 6; i++) {if (arr[i] == 0){return x;}if (arr[i] == number){x = false;return x;}}return x;}//生成随机中奖号码public static int[] creatLucyNumbers(){Random r = new Random();int[] Lucys= new int[7];//随机红球号码for (int i = 0; i < Lucys.length - 1; i++) {while (true) {int LucyNumber = r.nextInt(33) + 1;if (chRe(Lucys, LucyNumber)) {Lucys[i] = LucyNumber;break;}}}//随机蓝球号码Lucys[6] = r.nextInt(16) + 1;;return Lucys;}//检查中奖情况public static void judge(int[] userNUmbers,int[] lucyNumbers){int redcount = 0;int bluecount = 0;//判断红球中奖数量for (int i = 0; i < userNUmbers.length - 1; i++) {for (int j = 0; j < lucyNumbers.length - 1; j++) {if (userNUmbers[i] == lucyNumbers[j]){redcount++;break;}}}//判断蓝球是否命中bluecount = userNUmbers[6] == lucyNumbers[6] ? 1 : 0;//判断中奖情况if (bluecount == 1){switch (redcount){case (6):System.out.println("恭喜中奖,金额为:10000000元!");break;case (5):System.out.println("恭喜中奖,金额为:3000元!");break;case (4):System.out.println("恭喜中奖,金额为:200元!");break;case (3):System.out.println("恭喜中奖,金额为:10元!");break;case (2):case (1):case (0):System.out.println("恭喜中奖,金额为:5元!");break;}}else {switch (redcount){case (6):System.out.println("恭喜中奖,金额为:5000000元!");break;case (5):System.out.println("恭喜中奖,金额为:200元!");break;case (4):System.out.println("恭喜中奖,金额为:10元!");break;case (3):case (2):case (1):case (0):System.out.println("没中~~~");}}}
}
http://www.jsqmd.com/news/321195/

相关文章:

  • 合肥研究生留学中介为何零差评?跻身十强的秘诀分享
  • 2026智能眼镜电池哪家做的好?行业优质选择推荐
  • 第15篇 | 安全审计与日志分析:数字世界的“黑匣子”与“预言家”
  • 西安装修设计口碑厂商2026榜单:选对不选贵,现代简约/贴砖/庭院/橡胶木板/商混/餐边柜,装修设计直销厂家口碑排行榜单
  • 快客之家产品大揭秘,服务优势显著哪家性价比高
  • 济南硕士留学机构口碑排名发布!专业指导助您留学选择
  • 2026工业厂房环保工程设计施工一体化承包推荐,专业团队全程护航
  • 周六福2026焕新发布会盛大举行,“见宝非遗”系列产品首发
  • uniapp微信小程序php python的校园二手商城
  • uniapp微信小程序php python的校园生活服务 跑腿,平台
  • 揭秘济南硕士留学中介前十,性价比高为何重要?深度解读
  • 【2025榜单】推荐几家权威的气体检测仪生产厂家及品牌
  • 2026年汕头靠谱的威盛达玩具品牌推荐,威盛达玩具有限公司实力揭秘
  • 细聊三峡游轮船票性价比高的游轮品牌,三峡游轮船票价格贵吗
  • 2026年气体检测仪三大生产厂家权威盘点:技术实力与市场口碑双认证
  • 郑州留学中介哪家最好?录取案例多,服务全面专业
  • uniapp微信小程序php python的教室预约系统
  • 聚焦气体检测仪技术创新趋势,2026年优质生产厂家推荐
  • 聊聊江南电缆制造企业,绍兴地区可靠的有哪些?
  • uniapp微信小程序php python的旅游主题开发app
  • 2026国内软包电池厂家哪家好?行业实力企业解析
  • 气体检测仪哪家质量好?3家实力生产厂家实测拆解,适配所有工业/环保场景
  • 气体检测仪优质厂家盘点:五家品牌实力解析与选购参考
  • uniapp微信小程序php python的健身俱乐部课程预订系统 场地预约 系统
  • 细聊花悠花精油市场地位,选购时怎样判断是否靠谱?
  • 周六福铂金怎么选?从款式到性价比,帮你挑出最合适的那一款
  • DevOps流水线优化:基于GitLab CI/CD的自动化测试与部署策略
  • uniapp微信小程序php python的图书馆图书借阅管理系统_52v82
  • 采购指南:如何选择?这些主流气体检测仪生产厂家各有何千秋
  • 拓竹破百亿、捐一亿!陶冶:成绩属于每一位3D打印“竹子”