12、雙色球規則:雙色球每注投注號碼由6個紅色球號碼和1個藍色球號碼組成。紅色球號碼從1—33中選擇;藍色球號碼從1—16中選擇;請隨機生成一注雙色球號碼。(要求同色號碼不重複)java
package day03;數組
import java.util.HashSet;dom import java.util.Random;spa import java.util.Scanner;排序 import java.util.TreeSet;ci
public class JiHeShunagseqiu {input static int lanqiu1 = 0;hash public static void main(String[] args){table //系統一運行就會觸發隨機生成籃球和紅球class play(); } public static void play(){ Scanner input=new Scanner(System.in); System.out.println("=========模擬彩票大樂透========="); System.out.println("請用戶選擇出票的模式:1【機選】===== 2【手選】"); String num=input.next(); //利用parseInt轉換爲數字 int temp=Integer.parseInt(num); if(temp==1){ System.out.println("請選擇注數"); int zhu=input.nextInt(); Jixuan(zhu);
}else if(temp==2){//則證實是手選
} } //系統隨機生成的中獎號碼 public static HashSet Meth(){
Random ran = new Random(); HashSet<Integer> hong = new HashSet<Integer>(); while(hong.size()<6) { int hongqiu=ran.nextInt(33)+1; hong.add(hongqiu); }
System.out.print("系統生成的紅球號碼是: "); //第一處---------------------排序輸出---------------------------- //把紅球放入集合返回 TreeSet treeSet = new TreeSet(); treeSet.addAll(hong); System.out.print(treeSet); lanqiu1= ran.nextInt(16) + 1;
/*--------- for(Integer i:hong) { System.out.print(i+" "); } -----------------------*/ System.out.print("系統生成的籃球號碼是: "); System.out.print(lanqiu1+""); return hong; } // 用戶隨機生成的中獎的號碼 public static HashSet Jixuan(int zhushu){ //求兩個集合中相同的元素 HashSet hashSethong = new HashSet();//-----------用於每次返回的集合 int blue[]=new int[zhushu];//定義數組用於存儲藍色球 for(int i=0;i<zhushu;i++){
Random ran = new Random(); HashSet<Integer> jihong = new HashSet<Integer>(); while(jihong.size()<6){ int hongqiu=ran.nextInt(33)+1; jihong.add(hongqiu); } int lanqiu=ran.nextInt(16)+1; System.out.print("第"+(i+1)+"次隨機生成的紅球號碼是: ");
//------------------排序輸出------------------- //不容許存放相同的元素,對元素進行排序 TreeSet treeSet1 = new TreeSet(); treeSet1.addAll(jihong); System.out.print(treeSet1);
/* for(Integer s:jihong) { System.out.print(s+" "); }*/ System.out.print(" 隨機生成的籃球號碼是: "); System.out.print(lanqiu+""); System.out.println(); blue[i] = lanqiu; //把紅球給集合 hashSethong=jihong; //----------清空-------------- result(jihong,Meth(),blue[i]); jihong.clear();
}
return null; } //是否中獎? public static void result(HashSet<Integer> set1,HashSet<Integer> set2,int blue) { ////求兩個集合中相同的元素 HashSet sameSet = new HashSet(); for (Integer integer :set1){ //判斷是否包含 if (set2.contains(integer)){ sameSet.add(integer); } } //反回紅球的個數 int rowredsum=sameSet.size(); //籃球 int rowbluesum = 0; if(blue==lanqiu1){ rowbluesum=1; } System.out.print("\n本次紅球中了"+rowredsum+"個,"+"中的號碼是:"+sameSet); System.out.print("\n本次藍球中了"+rowbluesum+"個,"+"中的號碼是:"+rowbluesum); if (rowredsum == 6 && rowbluesum == 1) { System.out.print("-----恭喜您中了【一等獎】-------"); } else if (rowredsum == 6 && rowbluesum == 0) { System.out.println("-----恭喜您中了【二等獎】-------"); } else if (rowredsum == 5 && rowbluesum == 1) { System.out.println("-----恭喜您中了【三等獎】-------"); } else if ((rowredsum==5&&rowbluesum==0)||(rowredsum==4&&rowbluesum==1)) { System.out.println("-----恭喜您中了【四等獎】-------"); } else if ((rowredsum==4&&rowbluesum==0)||(rowredsum==3&&rowbluesum==1)) { System.out.println("-----恭喜您中了【五等獎】-------"); } else if ((rowredsum==2&&rowbluesum==1)||(rowredsum==1&&rowbluesum==1)||(rowredsum==0&&rowbluesum==1)) { System.out.println("-----恭喜您中了【六等獎】-------"); } else { System.out.println("-----很遺憾,您沒有中獎,在接在勵-------"); }
} } |