導航:首頁 > 編程語言 > javaif隨機抽獎

javaif隨機抽獎

發布時間:2023-06-29 13:02:07

java代碼實現抽獎:從班級的學號中抽出一個一等獎,兩個二等獎,三個三等獎

抽取問題, 重點是 同一個學號不能重復被抽取.

解決辦法很多,

  1. 比如數組可以使用下標來標記,號碼是否被使用,使用了就繼續下一次抽取

  2. 也可以使用集合來抽取,把集合順序打亂,然後隨便抽幾個就可以了

參考代碼:數組法

importjava.util.Random;

publicclassTest{

publicstaticvoidmain(String[]args){
intstuNums=30;

int[]nums=newint[stuNums];//存儲學號的數組
boolean[]flags=newboolean[stuNums];//標記,用於標記對應下標的學號是否已經被抽取過了
for(inti=0;i<stuNums;i++){
nums[i]=i+1;//給學號賦值
}

Randomr=newRandom();
while(true){
intindex=r.nextInt(stuNums);
if(!flags[index]){
System.out.println("A等:"+nums[index]);
flags[index]=true;//標記已經被使用過了
break;
}
}


for(inti=0;i<2;i++){
intindex=r.nextInt(stuNums);
if(!flags[index]){
System.out.println("B等:"+nums[index]);
flags[index]=true;
}else{
i--;//如果已經被抽取過了,那麼i建議,再次循環
}
}

for(inti=0;i<3;i++){
intindex=r.nextInt(stuNums);
if(!flags[index]){
System.out.println("c等:"+nums[index]);
flags[index]=true;
}else{
i--;
}
}
}
}

集合法

importjava.util.ArrayList;
importjava.util.Collections;

publicclassTest2{
publicstaticvoidmain(String[]args){
intstuNums=20;
ArrayList<Integer>list=newArrayList<Integer>();
for(inti=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));
}
}

❷ 用java完成一個抽獎的程序。 每次運行程序,都會從以下的抽獎結果中隨機顯示一個出來:

生成100個對象,對象有個屬性,其中10個是大獎,40個是小獎,50個是無獎。
放到一個List里。
每次抽中的步驟
1、隨機生成0-List長度之間的數值 ,去取List中的相應對象,並移除這個對象。

代碼如下。:
獎品對象類:

public class PrizeBean {

private String type;

public String getType() {
return eggType;
}

public void setType(String eggType) {
this.eggType = eggType;
}

}
獎品池初始化代碼段:

{
List prizebeanList = new ArrayList();

for (int i = 0; i < 10; i++) {
PrizeBean prizeBean = new PrizeBean();

prizeBean.setType(「大獎「);

prizebeanList.add(prizeBean);
}

for (int i = 0; i < 40; i++) {
PrizeBean prizeBean = new PrizeBean();

prizeBean.setType(「小獎「);

prizebeanList.add(prizeBean);
}

for (int i = 0; i < 50; i++) {
PrizeBean prizeBean = new PrizeBean();

prizeBean.setType(「無獎「);

prizebeanList.add(prizeBean);
}

}
抽獎代碼段:

/**
*獎品池已經空的,肯定返回無獎了。。。

**/

if(prizebeanList.size()==0){
- 沒有中獎哦,下次加油!
return;

}
/**
* 隨機生成,獎品池中獎品數量的數字。。取出獎品池中的數字。。移除記錄。返回。。
*/
int resultnum = (int) (Math.random() * prizebeanList.size());
PrizeBean resultPrizeBean = prizebeanList.get(resultnum);
prizebeanList.remove(resultPrizeBean);

if(resultPrizeBean.getType() .eqauls("大獎"){
- 恭喜,大獎!

}else if(resultPrizeBean.getType() .eqauls("小獎"){
- 運氣不錯哦,小獎!

}else{
- 沒有中獎哦,下次加油!
}.

❸ 關於java抽獎代碼

intSjs=newRandom().nextInt(10)+1;
if(Sjs==1)
{
System.out.println("您抽到的是一等獎");
}
elseif(Sjs>1&&Sjs<4)
{
System.out.println("您抽到的是二等獎");
}
elseif(Sjs>3&&Sjs<7)
{
System.out.println("您抽到的是三等獎");
}
else
{
System.out.println("謝謝參與");
}

你這個if判斷語句後面是沒有分號的,

❹ 商場推出幸運抽獎活動的java初級代碼編寫

public class Lucky {
public static void main(String[] args){
System.out.println("請輸入您的4位會員卡號:");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt(); //接收用戶從控制台輸入的會員卡號,並保存在會員卡號變數中
int a = number/1000; //千位
int b = number%1000/100; //百位
int c = number%100/10; //十位
int d = number%10; //個位
if((a+b+c+d)>20){
System.out.println("恭喜中獎!您是幸運客戶");
}else{
System.out.println("謝謝參與!");
}
}
}

最基礎的 沒有異常判斷 無限循環輸入什麼東西

閱讀全文

與javaif隨機抽獎相關的資料

熱點內容
資料庫查詢系統源碼 瀏覽:617
php5314 瀏覽:358
完美國際安裝到哪個文件夾 瀏覽:669
什麼app可以掃一掃做題 瀏覽:540
程序員編碼論壇 瀏覽:924
淘點是什麼app 瀏覽:660
中國高等植物pdf 瀏覽:454
51單片機時間 瀏覽:182
後台如何獲取伺服器ip 瀏覽:267
單片機流水燈程序c語言 瀏覽:235
程序員第二職業掙錢 瀏覽:240
運行里怎麼輸入伺服器路徑 瀏覽:843
pythonstepwise 瀏覽:510
劉一男詞彙速記指南pdf 瀏覽:66
php認證級別 瀏覽:370
方舟編譯啥時候推送 瀏覽:1012
php手機驗證碼生成 瀏覽:677
哲學思維pdf 瀏覽:17
凌達壓縮機有限公司招聘 瀏覽:535
weblogic命令部署 瀏覽:39