
正文
彩票源代码java 彩票源代码网站
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用Java程序写一个彩票的模拟程序生成6个红球(1-33的随机数),1个蓝球(1-16的随机数),随机数可以重复
import java.util.Random;
import java.util.Scanner;
public class DoubleBalls {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("是否机选一组双色球号码?(Y/N)");
Scanner input = new Scanner(System.in);
char a = input.next().charAt(0); // 输入一个char字符,0位即是第一位
if (a == 'Y' || a == 'y') {
Random suiji = new Random(); // 创建suiji方法
int blue = suiji.nextInt(16);//从0到16中选一个数字个蓝球
while(blue==0){
blue=suiji.nextInt(16);
}//如果选到了0,再选一次给blue
int red[] = new int[6];// 用一个6个元素的数组装红球
for (int i = 0; i 6; i++) {
red[i] = suiji.nextInt(33); // 随机分配0到33的整数
if (red[i] == 0) {i--;}
if (i == -1) {i = 0;}
for (int j = 0; j i; j++) {
while (red[i] == red[j]) {i--;}// 发现相同的从新回去生成一个
}
}
System.out.print("红球:");
for (int k = 0; k red.length; k++) {
System.out.print(red[k] + " ");// 输出数组red
}
System.out.print("蓝球:"+blue);
} else
System.out.println("fuck you~");
}
}
应该可以了,可以产生一组。。。如果楼主你中头奖了,送我套房好了哈^0^
相关问答
Q1: java程序编写双色球代码
截图彩票源代码java:
选取红色球号码方法:
/**
* 随机选取红色球
*
* 获取1~33其中一个数
*
* 0 = Math.random 1
*/
private int randomOneRedValue() {
int randomValue = (int) (Math.random() * 33 + 1);
return randomValue;
}
选取蓝色球号码方法:
/**
* 随机选取蓝色球号码
*
* 获取1~16的数值
*
* @return
*/
private int randomOneBlueValue() {
int randomValue = (int) (Math.random() * 15 + 1);
return randomValue;
}
测试:
public class Test1 {
public static void main(String[] arg) {
Test1 localTest1 = new Test1();
// 双色球 :红色球号码 + 蓝色球号码
// 六个红色球和一个蓝色球号码
// 红色球号码从1~33中选择
// 蓝色球号码从1~16中选择
// 一等奖:七个号码相符(六个红色号码和一个蓝色球号码)(红色球号码顺序不限彩票源代码java,下同)
// 二等奖:六个红色球号码相符;
// 三等奖:五个红色球号码彩票源代码java,或者四个红色球号码和一个蓝色球号码相符彩票源代码java;
// 四等奖:五个红色球号码彩票源代码java,或者三个红色球号码和一个蓝色球号码相符;
// 五等奖:四个红色球号码,或者三个红色球号码和一个蓝色球号码相符;
// 六等奖:一个蓝色球号码相符(有误红色球号码相符均可);
// 例如:红色球号码 01 06 13 19 24 28 蓝色球号码 16
System.out.println("开始出奖");
// 定义双色球数组,大小为7
String[] values = new String[7];
for (int i = 0; i 7; i++) {
if (i == 6) {
int blueValue = localTest1.randomOneBlueValue();
if (blueValue 10) {
values[i] = "0" + blueValue;
} else {
values[i] = String.valueOf(blueValue);
}
} else {
int redValue = localTest1.randomOneRedValue();
if (redValue 10) {
values[i] = "0" + redValue;
} else {
values[i] = String.valueOf(redValue);
}
}
}
System.out.println();
System.out.println("出奖结束");
System.out.println();
System.out.print("双色球开奖号码:");
// 打印双色球号码
for (String value : values) {
System.out.print(" " + value);
}
}
}
Q2: 3D彩票JAVA编程怎么写?
import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.Scanner; public class Lotto { public static final int NUMBER = 7; public static void main(String[] args) { ListInteger userNumbers = new ArrayListInteger(); Scanner scanner = new Scanner(System.in); int i = 1; while(i 8) { System.out.println("输入第" + i + "个数字: "); userNumbers.add(scanner.nextInt()); i ++; } System.out.println(prize(userNumbers.toArray(new Integer[NUMBER]))); } private static Integer[] generateLotto() { ListInteger lottoNumbers = new ArrayListInteger(); Random rand = new Random(); int lottoNumber, i = 0; while(i NUMBER) { lottoNumber = rand.nextInt(35) + 1; if(!lottoNumbers.contains(lottoNumber)) { lottoNumbers.add(lottoNumber); i ++; } } return bubbleSort(lottoNumbers.toArray(new Integer[NUMBER])); } private static Integer[] bubbleSort(Integer[] array) { for(int i = 0; i array.length; i ++) { for(int j = 0; j array.length - i - 1; j ++) { if(array[j] array[j + 1]) { int temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; } } } return array; } public static String prize(Integer[] userNumbers) { int count = 0; Integer[] lottoNumbers = generateLotto(); for(Integer i : userNumbers) { for(Integer j : lottoNumbers) { if (i == j) { count ++; } } } return "猜对了 "+ count + " 个彩票号码"; }}
输入第1个数字:
5
输入第2个数字:
14
输入第3个数字:
19
输入第4个数字:
24
输入第5个数字:
33
输入第6个数字:
34
输入第7个数字:
27
猜对了 3 个彩票号码
关于彩票源代码java和彩票源代码网站的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






