完整代碼為:
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、南宮網(wǎng)站維護(hù)、網(wǎng)站推廣。
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("開(kāi)始抽取紅球!");
while (getMoreRed) {
getAgain = false;
int red = random.nextInt(36) + 1;
System.out.print("本次抽取到的紅球?yàn)椋篬" + red + "]!");
for (int i = 0; i index; i++) {
if (redBalls[i] == red) {
System.out.print("重復(fù)抽取,將重新抽取紅球");
getAgain = true;
break;
}
}
System.out.println("");
if (getAgain){
continue;
}
redBalls[index - 1] = red;
index++;
getMoreRed = index 7;
}
System.out.println("抽取到的紅球?yàn)椋?);
Arrays.sort(redBalls);
for (int redBall : redBalls) {
System.out.print(redBall + " ");
}
System.out.println("\n\n開(kāi)始抽取藍(lán)球!");
System.out.println("本次抽取到的藍(lán)球?yàn)椋篬" + (random.nextInt(16) + 1) + "]!");
}
}
運(yùn)行結(jié)果:
普通抽取:
重復(fù)時(shí)抽?。?/p>
public class test {\x0d\x0a public static void main(String[] args) {\x0d\x0a //定義人名數(shù)組\x0d\x0a String [] name = {"張三","李四","王五","八神庵","不知火舞","大蛇","景天","唐雪見(jiàn)","李逍遙","趙靈兒"};\x0d\x0a//隨機(jī)生成數(shù)組下標(biāo)、\x0d\x0a int num = (int)(Math.random() * 1000);\x0d\x0a//對(duì)生成的隨機(jī)數(shù)進(jìn)行判斷,如果小于數(shù)組下標(biāo),就跳出循環(huán)\x0d\x0awhile (numname.length-1) {\x0d\x0a if (num
回答于?2022-11-16
抽取問(wèn)題, 重點(diǎn)是 同一個(gè)學(xué)號(hào)不能重復(fù)被抽取.
解決辦法很多,
比如數(shù)組可以使用下標(biāo)來(lái)標(biāo)記,號(hào)碼是否被使用,使用了就繼續(xù)下一次抽取
也可以使用集合來(lái)抽取,把集合順序打亂,然后隨便抽幾個(gè)就可以了
參考代碼:數(shù)組法
import?java.util.Random;
public?class?Test?{
public?static?void?main(String[]?args)?{
int?stuNums=30;
int[]?nums=new?int[stuNums];//存儲(chǔ)學(xué)號(hào)的數(shù)組
boolean[]?flags=new?boolean[stuNums];//標(biāo)記,用于標(biāo)記對(duì)應(yīng)下標(biāo)的學(xué)號(hào)是否已經(jīng)被抽取過(guò)了
for?(int?i?=?0;?i??stuNums;?i++)?{
nums[i]=i+1;//給學(xué)號(hào)賦值
}
Random?r=new?Random();
while(true){
int?index?=?r.nextInt(stuNums);
if(!flags[index]){
System.out.println("A等:"+nums[index]);
flags[index]=true;?//標(biāo)記已經(jīng)被使用過(guò)了
break;
}
}
for?(int?i?=?0;?i??2;?i++)?{
int?index?=?r.nextInt(stuNums);
if(!flags[index]){
System.out.println("B等:"+nums[index]);
flags[index]=true;
}else{
i--;//如果已經(jīng)被抽取過(guò)了?,那么i建議,再次循環(huán)
}
}
for?(int?i?=?0;?i??3;?i++)?{
int?index?=?r.nextInt(stuNums);
if(!flags[index]){
System.out.println("c等:"+nums[index]);
flags[index]=true;
}else{
i--;
}
}
}
}
集合法
import?java.util.ArrayList;
import?java.util.Collections;
public?class?Test2?{
public?static?void?main(String[]?args)?{
int?stuNums=20;
ArrayListInteger?list=new?ArrayListInteger();
for?(int?i?=?0;?i??stuNums;?i++)?{
list.add(i+1);
}
System.out.println("有序"+list);
Collections.shuffle(list);//打亂順序
System.out.println("亂序"+list);
System.out.println("A等"+list.get(0));
System.out.println("B等"+list.get(1));
System.out.println("B等"+list.get(2));
System.out.println("C等"+list.get(3));
System.out.println("C等"+list.get(4));
System.out.println("C等"+list.get(5));
}
}