本帖最后由 小东先森 于 2016-9-11 17:34 编辑
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
/*
* 我这个彩票生成的数字是不重复的。
* 模拟彩票:
随机生成6个0-15的数字,再从键盘录入6个0-15的数字,
当随机生成的数字和键盘录入的数字:
3个一样,中三等奖
4个一样,中二等奖
5个一样,中一等奖
全部一样,中特等奖
*/
public class Test {
public static void main (String[] args) {
System.out.println("游戏开始...");
ArrayList<Integer> arr = creatRandom();
ArrayList<Integer> newArr = buildRandom();
//System.out.println(newArr);
//显示一下生成的数
System.out.println("本期中奖号码为:");
System.out.println(arr);
compare(arr,newArr);
} //第一种方法用HashSet集合生成6个随机数并储存
//生成六个随机数并储存起来用HashSet集合存储
/*public static HashSet<Integer> creatRandom() {
Random r = new Random();
//ArrayList<Integer> arr = new ArrayList<Integer>();
HashSet<Integer> newArr = new HashSet<Integer>();
while(newArr.size() < 6) {
int luckyNum = r.nextInt(16);
System.out.println(luckyNum);
newArr.add(luckyNum);
}
return newArr;
}*/
//第二种方法用ArrayList集合存储六个随机数
public static ArrayList<Integer> creatRandom() {
Random r = new Random();
//ArrayList<Integer> arr = new ArrayList<Integer>();
ArrayList<Integer> newArr = new ArrayList<Integer>();
while(newArr.size() < 6) {
int luckyNum = r.nextInt(16);
if(!newArr.contains(luckyNum)) {
newArr.add(luckyNum);
}
}
return newArr;
}
//键盘录入六个数字也储存起来
public static ArrayList<Integer> buildRandom() {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> arr = new ArrayList<Integer>();
int i = 0;
int j = 1;
while(arr.size() < 6) {
System.out.println("----------------------");
System.out.println("请输入0-15中任意一个整数:");
int num = sc.nextInt();
if(num>15 || num < 0) {
System.out.println("----------------------");
System.out.println("您输入的数字有误,请重新输入...");
}else if(!arr.contains(num)) {
arr.add(num);
System.out.println("----------------------");
System.out.println("您已经正确输入"+(j++)+"个数字了");
}else {
System.out.println("不可输入重复数字,请重新输入...");
}
i++;
System.out.println("---------------------");
System.out.println("这是你第"+i+"次输入数字");
}
System.out.println("----------------");
System.out.println("输入完毕");
return arr;
}
//将两组数据进行比较,判断并打印结果
//arr表示随机的数,newArr表示用户输入的数
public static void compare(ArrayList<Integer> arr,ArrayList<Integer> newArr) {
int count = 0;
for(int i = 0;i < arr.size();i++) {
if(newArr.contains(arr.get(i))) {
count++;
}
}
int luckyNum01 = newArr.get(0);
int luckyNum02 = newArr.get(1);
int luckyNum03 = newArr.get(2);
int luckyNum04 = newArr.get(3);
int luckyNum05 = newArr.get(4);
int luckyNum06 = newArr.get(5);
switch (count) {
case 3:
System.out.println("您的号码为"+luckyNum01+","+luckyNum02+","+luckyNum03+","+luckyNum04+","+luckyNum05+","+luckyNum06+",中了三等奖,恭喜您!");
break;
case 4:
System.out.println("您的号码为"+luckyNum01+","+luckyNum02+","+luckyNum03+","+luckyNum04+","+luckyNum05+","+luckyNum06+",中了二等奖,恭喜您!");
break;
case 5:
System.out.println("您的号码为"+luckyNum01+","+luckyNum02+","+luckyNum03+","+luckyNum04+","+luckyNum05+","+luckyNum06+",中了一等奖,恭喜您!");
break;
case 6:
System.out.println("您的号码为"+luckyNum01+","+luckyNum02+","+luckyNum03+","+luckyNum04+","+luckyNum05+","+luckyNum06+",中了特等奖,恭喜您!");
break;
default:
System.out.println("您的号码为"+luckyNum01+","+luckyNum02+","+luckyNum03+","+luckyNum04+","+luckyNum05+","+luckyNum06+",没有中奖很遗憾...");
break;
}
}
}
|
|