Java实现双色球(选号+开奖)
作者:互联网
package test01;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class fname {
static String[] storage = new String[100];
static int k = 0;
public static int[] GenrateWinningNub() {
int[] twn = new int[6];
int[] wn = new int[7];
Boolean[] bool = new Boolean[34];
for (int j = 0; j < 34; j++) {
bool[j] = false;
}
for (int i = 0; i < 6; i++) {
int temp = new Random().nextInt(33) + 1;// 產生1-33
if (bool[temp] == false) {// 沒有產生過
twn[i] = temp;
bool[temp] = true;
} else
i--;
}
Arrays.sort(twn);
System.arraycopy(twn, 0, wn, 0, twn.length);
wn[6] = new Random().nextInt(16) + 1;
return wn;
}
public static void Menu() {
System.out.println("請選擇購買方式:1.自选号码投注 2.机选号码投注 3.退出");
@SuppressWarnings("resource")
int input = new Scanner(System.in).nextInt();
switch (input) {
case 1:
storage[k++] = SelfChoice();
break;
case 2:
storage[k++] = ComputerChoice();
break;
case 3: {
System.out.println("選購結束,您選購的號碼如下,祝您中獎");
for (String str : storage) {
if (str == null)
break;
System.out.println(str);
}
System.out.println();
return;
}
}
Menu();
}
public static String ComputerChoice() { // 電腦產生一組隨機號碼
System.out.println("計算機已經為您選好一組號碼:");
int[] temp = GenrateWinningNub();
String str = "";
for (int i : temp) {
str += String.valueOf(i) + " ";
}
System.out.println(str);
System.out.println("請輸入投注倍數:");
@SuppressWarnings("resource")
String df = new Scanner(System.in).nextLine();
str += "倍數:" + df;
System.out.println(str);
return str;
}
@SuppressWarnings("resource")
public static String SelfChoice() {
System.out.println("請輸入你要選擇的號碼 中間以一個空格為分割 最後加註數");
String str = new Scanner(System.in).nextLine();
System.out.println("請輸入投注倍數:");
str = str + " 倍數:" + new Scanner(System.in).nextLine();
System.out.println(str);
return str;
}
public static int[][] ChangeData() {// 將storage轉化為int[][]二維數組
int[][] choiced = new int[storage.length][8];
for (int i = 0; i < choiced.length; i++) {
if (storage[i] == null) {
break;
}
String[] str = storage[i].split(" ");
for (int j = 0; j < choiced[0].length; j++)// j<6
{
if (j < 7) {
choiced[i][j] = Integer.valueOf(str[j]);
} else {
choiced[i][j] = Integer.parseInt(str[j].split(":")[1]);
}
}
}
return choiced;
}
public static void VerifyWinning(int[][] choicednub, int[] winnub) {// 驗證是否中獎以及中獎金額
int Samered = 0;
int Sameblue = 0;
int Getbonus = 0;// 獎金
int cost = 0;
for (int i = 0; i < choicednub.length; i++) {
if (choicednub[i][0] == 0) {
break;
}
for (int j = 0; j < choicednub[0].length - 2; j++) {
for (int m = 0; m < 5; m++) {
if (winnub[m] == choicednub[i][j]) {
Samered++;
}
}
}
if (choicednub[i][6] == winnub[6]) {
Sameblue = 1;
}
if (Sameblue == 0 && Samered <= 3) {
System.out.println("第" + (i + 1) + "組號碼未中獎");
}
if (Sameblue == 1 && Samered <= 2) {
Getbonus += 5 * choicednub[i][7];
System.out.println("第" + (i + 1) + "組號碼已中六等獎,中奖金额为倍數 "+choicednub[i][7]+"*5=" + 5 * choicednub[i][7] + " 元");
}
if ((Sameblue == 0 && Samered == 4) || (Sameblue == 1 && Samered == 3)) {
Getbonus += 10 * choicednub[i][7];
System.out.println("第" + (i + 1) + "組號碼已中五等獎,中奖金额为倍數*10=" + 10 * choicednub[i][7] + " 元");
}
if ((Sameblue == 0 && Samered == 5) || (Sameblue == 1 && Samered == 4)) {
Getbonus += 200 * choicednub[i][7];
System.out.println("第" + (i + 1) + "組號碼已中%%%%%%%%%%%%%%%%四等獎,中奖金额为倍數*200="
+ 200 * choicednub[i][7] + " 元");
}
if (Sameblue == 1 && Samered == 5) {
Getbonus += 3000 * choicednub[i][7];
System.out.println("第" + (i + 1) + "組號碼已中@@@@@@@@@@@@@@@@@三等獎,中奖金额为倍數*3000="
+ 3000 * choicednub[i][7] + " 元");
System.out.println(Arrays.toString(choicednub[i]));
}
if (Sameblue == 0 && Samered == 6) {
Getbonus += 2000000 * choicednub[i][7];
System.out.println("第" + (i + 1) + "組號碼已中$$$$$$$$$$$$$$$$$二等獎,中奖金额为倍數*2000000="
+ 2000000 * choicednub[i][7] + " 元");
System.out.println(Arrays.toString(choicednub[i]));
}
if (Sameblue == 1 && Samered == 6) {
Getbonus += 5000000 * choicednub[i][7];
System.out.println("第" + (i + 1) + "組號碼已中******************一等獎,中奖金额为倍數*5000000="
+ 5000000 * choicednub[i][7] + " 元");
System.out.println(Arrays.toString(choicednub[i]));
}
cost += choicednub[i][7];
Samered = 0;
Sameblue = 0;
}
System.out.println();
System.out.println("您本次消費: " + cost * 2 + " 元,中獎 " + Getbonus + " 元");
if (Getbonus > cost * 2) {
System.out.println("您最終淨賺: " + (Getbonus - cost * 2) + " 元,祝願您下次中獎");
}
if (Getbonus < cost * 2) {
System.out.println("您最終虧損: " + (cost * 2 - Getbonus) + " 元,祝願您下次中獎");
}
if (Getbonus == cost * 2) {
System.out.println("您最終不賺也不虧,祝願您下次中獎");
}
}
public static int[][] findbest(int count) {
int[][] a = new int[count][8];
for (int i = 0; i < count; i++) {
int[] wn = GenrateWinningNub();
System.out.println("第 " + (i + 1) + " 組 :" + Arrays.toString(wn));
for (int j = 0; j < 7; j++) {
a[i][j] = wn[j];
}
a[i][7] = 1;
}
return a;
}
public static void showwinnub(int[] winnub) {
System.out.println("本次開獎號碼為:");
for (int a : winnub) {
System.out.print(a + " ");
}
System.out.println();
}
public static void main(String[] args) {
Menu();// 選擇
int[][] shownub = ChangeData();// 二維int[][]數組記錄號碼以及倍數
int[] winnub = GenrateWinningNub();// 產生中獎號碼
showwinnub(winnub);
VerifyWinning(shownub, winnub);
//VerifyWinning(findbest(10), winnub);
}
}
标签:Java,int,choicednub,System,选号,开奖,str,println,out 来源: https://blog.csdn.net/qq_43141128/article/details/120611631