🏡个人主页 :@ 守夜人st
🚀系列专栏:Java
…持续更新中敬请关注…
🙉博主简介:软件工程专业,在校学生,写博客是为了总结回顾一些所学知识点
写在开头
- 模拟彩票排三
- 中奖截图
- 模拟双色球开奖
- 中奖截图
假期再次复习了一遍Java基础语法,写了两个小程序给大家分享一下(下面附上源码,程序有待优化,不足之处请指正,喷子勿扰!!!)
模拟彩票排三
package com.shouyeren.array;import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;/*
* 模拟彩票排三开奖游戏
* 1.生成0——10的随机数3个
* 2.用户输入三个数
* 3.若用户输入与生成的随机数一致(数字及顺序一致),则用户中奖,否则未中奖
* */
public class LotteryGame {public static void main(String[] args) {int[] num1 = luckNumber();int[] num2 = userNumber();compare(num1,num2);}public static void compare(int[] num1,int[] num2){OUT:while (true){for (int i = 0; i < num1.length; i++) {if (num2[i] != num1[i]){System.out.println("很遗憾您并没有中奖!!!");System.out.println("开奖号码为:" + Arrays.toString(num1));System.out.println("您的号码为:" + Arrays.toString(num2));//定义标签跳出大循环break OUT;} else if (num2[i] == num1[i]){System.out.println("开奖号码为:" + Arrays.toString(num1));System.out.println("您的号码为:" + Arrays.toString(num2));System.out.println("恭喜您中奖了,您中奖的号码为: " + Arrays.toString(num1));break OUT;}}}}public static int[] luckNumber(){int[] num1 = new int[3];Random r = new Random();//生成三个随机数并存入数组for (int i = 0;i < num1.length;i++) {num1[i] = r.nextInt(11);}return num1;}public static int[] userNumber(){//获取用户输入Scanner s = new Scanner(System.in);int[] num2 = new int[3];System.out.println("请输入您的彩票号码:");for (int i = 0;i < 3;i++) {num2[i] = s.nextInt();}return num2;}
}
中奖截图
模拟双色球开奖
package com.shouyeren.programmingthinking;import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;/*
* 需求:模拟双色球
* 1,随机生成六个红色球(1——33不能重复),随机一个蓝色球号码(1——16)
* 2.用户输入7位的彩票号码
* 3.查询中奖情况
*
* */
public class BicolorSphere {public static void main(String[] args) {//生成一个开奖号码int[] luckNumber = createLuckNumber();//接收用户输入的号码int[] userNumber = userLuckNumber();//判断是否中奖compare(luckNumber,userNumber);}public static void compare(int[] luckNumber,int[] userNumber){int readHiuNumber = 0;int bluedHiuNumber = 0;for (int i = 0; i < userNumber.length-1; i++) {for (int j = 0; j < luckNumber.length-1; j++) {if (userNumber[i] == luckNumber[j]) {readHiuNumber++;break;}}}bluedHiuNumber = luckNumber[luckNumber.length-1] == userNumber[userNumber.length-1] ? 1 : 0;System.out.println("开奖号码为:" + Arrays.toString(luckNumber));System.out.println("您的号码为:" + Arrays.toString(userNumber));System.out.println("-----------------------------------");System.out.println("红色球号码中了 " + readHiuNumber + " 个...");System.out.println("蓝色球号码是否命中:" + (bluedHiuNumber == 1 ? "是..." : "否..."));if (bluedHiuNumber == 1 && readHiuNumber < 3){System.out.println("恭喜您中了5元!!!");} else if (bluedHiuNumber == 1 && readHiuNumber == 3 ||bluedHiuNumber == 0 && readHiuNumber == 4) {System.out.println("恭喜您中了10元!!!");}else if (bluedHiuNumber == 1 && readHiuNumber == 4 ||bluedHiuNumber == 0 && readHiuNumber == 5) {System.out.println("恭喜您中了200元!!!");}else if (bluedHiuNumber == 1 && readHiuNumber == 5) {System.out.println("恭喜您中了3000元!!!");}else if (bluedHiuNumber == 0 && readHiuNumber == 6) {System.out.println("恭喜您中了500万元!!!");}else if (bluedHiuNumber == 1 && readHiuNumber == 6) {System.out.println("恭喜您中了1000万元!!!");}else {System.out.println("很遗憾,您没有中奖。。。");}}public static int[] createLuckNumber(){int[] num = new int[7];Random r = new Random();for (int i = 0; i < num.length-1; i++) {while (true) {int data = r.nextInt(33)+1;boolean flag = true;for (int j = 0; j < i; j++) {if (num[j] == data){flag = false;break;}}if (flag) {num[i] = data;break;}}}//生成一个蓝色球号码num[num.length-1] = r.nextInt(16)+1;return num;}public static int[] userLuckNumber(){//初始化一个数组用来存储用户输入的号码int[] num = new int[7];Scanner sc = new Scanner(System.in);for (int i = 0; i < num.length-1; i++) {System.out.println("请您输入第" + (i+1) + "个红球号码(1——33,要求不重复)");num[i] = sc.nextInt();}//单独录入一个蓝色球号码System.out.println("请输入蓝色球号码(1——16):");num[num.length-1] = sc.nextInt();return num;}
}
中奖截图
哈哈,虽然不是真的,但是还是很开心!