重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
方法很多種,最簡單理解的就直接便利一下數組睜橘吵就伍此好了悉侍
創新互聯公司專業為企業提供晉城網站建設、晉城做網站、晉城網站設計、晉城網站制作等企業網站建設、網頁設計與制作、晉城企業網站模板建站服務,10余年晉城做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。
for(int n = 0 ; ni ; n++){
if(a[i]==a[n]){
continue;
}
}
換掉
if(a[i]==a[i-1])
continue;
截圖:
選取紅色球號碼方法:
/**
?*?隨機選取紅色球
?*?
?*?獲取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中選擇
//?一等獎:七個號碼相符(六個紅色號碼和一個藍色球號碼)(紅色球號碼順序不限,下同)
//?二等獎:六個紅色球號碼相符散蘆;
//?三等獎:五個紅色球號碼,或者四個紅色球號碼和一個藍色球號碼相符;
//?四等獎:五個紅色球號碼,或者三個紅色球沖團帶號碼和一個藍色球號碼相符;
//?五等獎:四個紅色球號碼,或者三個紅色球號碼和一個藍色球號碼相符;
//?六等獎:一個藍色球號碼相符(有誤紅色球號碼相符均可);
//?例如:紅色球號碼?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);
}
}
}
完整代碼為:
public class Main {
public static void main(String[] args) {
int index = 1;
int[] redBalls = new int[6];
Random random = new Random();
boolean getMoreRed = true;
boolean getAgain;
System.out.println("開始抽取紅球!");
while (getMoreRed) {
getAgain = false;
int red = random.nextInt(36) + 1;
System.out.print("本次抽取到的紅球為:[" + red + "]!");
for (int i = 0; i index; i++) {
if (redBalls[i] == red) {
System.out.print("重復抽取,將重仿運新抽取紅球");
getAgain = true;
break;
}
}
System.out.println("");
if (getAgain){
continue;
}
redBalls[index - 1] = red;
index++;
getMoreRed = index 7;
}
System.out.println("抽取到的蠢耐紅球為:");
Arrays.sort(redBalls);
for (int redBall : redBalls) {
System.out.print(redBall + " "備檔梁);
}
System.out.println("\n\n開始抽取藍球!");
System.out.println("本次抽取到的藍球為:[" + (random.nextInt(16) + 1) + "]!");
}
}
運行結果:
普通抽取:
重復時抽取:
public class sedasd {
static Random random = new Random();
public static void main(String[] args) {
System.out.println(Arrays.toString(getStor(getRed())));/*getRed()得到存儲取出的號碼的數組ns
getStor(世棚)核桐對數組的輸出格式進行格式化
*/
}
public static int[] getRed() {
int[] num = new int[33];
int[] ns = new int[6];
int index = 0;
for (int i = 0; i 100; i++) {
int temp = random.nextInt(33);
if (num[temp] == 0) { //判斷取改返坦出的號碼是不是第一次取出
ns[index] = temp + 1; //random.nextInt(33)是從零到32所以要加1
num[temp] = 1;
index++;
}
if (!(ns[5] == 0)) { //判斷是不是取6個號碼
return ns;
}
}
return ns;
}
public static String[] getStor(int[] s) {
String[] ns = new String[6];
Arrays.sort(s);
for (int i = 0; i s.length; i++) {
if (s[i] 10) {
ns[i] = "0" + s[i];
} else {
ns[i] = "" + s[i];
}
}
return ns;
}
}
希望可以幫到你,要是還不理解可以在問我。。。