import?java.util.Scanner;
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、做網(wǎng)站與策劃設(shè)計,長沙縣網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:長沙縣等地區(qū)。長沙縣做網(wǎng)站價格咨詢:18980820575
/**
*
*/
public?class?f?{
public?static?void?main(String?args[]){
Scanner?scan?=?new?Scanner(System.in);
System.out.print("請輸入抽獎號碼上限:");
int?max?=?scan.nextInt();
System.out.print("請輸入抽獎次數(shù):");
int?n?=?scan.nextInt();
System.out.print("中獎號碼依次為:");
for(int?i=0;in;i++){
System.out.print((int)(Math.random()*max+1)+"?");
}
}
}
抽取問題, 重點是 同一個學(xué)號不能重復(fù)被抽取.
解決辦法很多,
比如數(shù)組可以使用下標(biāo)來標(biāo)記,號碼是否被使用,使用了就繼續(xù)下一次抽取
也可以使用集合來抽取,把集合順序打亂,然后隨便抽幾個就可以了
參考代碼:數(shù)組法
import?java.util.Random;
public?class?Test?{
public?static?void?main(String[]?args)?{
int?stuNums=30;
int[]?nums=new?int[stuNums];//存儲學(xué)號的數(shù)組
boolean[]?flags=new?boolean[stuNums];//標(biāo)記,用于標(biāo)記對應(yīng)下標(biāo)的學(xué)號是否已經(jīng)被抽取過了
for?(int?i?=?0;?i??stuNums;?i++)?{
nums[i]=i+1;//給學(xué)號賦值
}
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)被使用過了
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)被抽取過了?,那么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));
}
}
生成100個對象,對象有個屬性,其中10個是大獎,40個是小獎,50個是無獎。
放到一個List里。
每次抽中的步驟
1、隨機生成0-List長度之間的數(shù)值 ,去取List中的相應(yīng)對象,并移除這個對象。
代碼如下。:
獎品對象類:
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);
}
}
抽獎代碼段:
/**
*獎品池已經(jīng)空的,肯定返回?zé)o獎了。。。
**/
if(prizebeanList.size()==0){
- 沒有中獎哦,下次加油!
return;
}
/**
* 隨機生成,獎品池中獎品數(shù)量的數(shù)字。。取出獎品池中的數(shù)字。。移除記錄。返回。。
*/
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{
- 沒有中獎哦,下次加油!
}.
我給你個比較簡單的,,但是需要按照你的要求進行稍微的修改。。然后在main方法中去執(zhí)行就可以了:
public class GoodLuck {
int custNo;
int i=1;
String answer;
String awardName;
public void LuckNo(){
Scanner input=new Scanner(System.in);
System.out.println("\n我行我素購物管理系統(tǒng) 幸運抽獎\n");
do{
// 需要的話請把隨機數(shù)調(diào)整成你想要的范圍(我這個是為了測試方便寫的1
(~3的隨機數(shù),根據(jù)你的需要把下面的3換成你想要的數(shù)字就行了)
int num=(int)(Math.random()*3+1);
System.out.print("請輸入會員卡號(4位整數(shù)):");
custNo=input.nextInt();
//百位數(shù)與隨機數(shù)相同的為幸運者
int bai=custNo/100%10;
while(i==1){
if(custNo=1000custNo=9999){
break;
}
else{
System.out.println("\n會員號碼輸入有誤,請重新輸入:");
custNo=input.nextInt();
continue;
}
}
if(bai==num){
showAward();
System.out.print("\n卡號:"+custNo+"是幸運客戶,獲得"+awardName);
}else{
System.out.print("\n卡號:"+custNo+"\t謝謝您的支持!");
}
System.out.println("\n是否繼續(xù)(y/n)");
answer=input.next();
while(i==1){
if(answer.equals("y")||answer.equals("n")){
break;
}else{
System.out.print("輸入有誤!請重新輸入:");
answer=input.next();
continue;
}
}
}while(!answer.equals("n"));
}
public void showAward(){
int num=(int)(Math.random()*3+1);
if(num==1){
awardName="Mp3";
}
else if(num==2){
awardName="美的微波爐";
}
else{
awardName="美的電飯鍋";
}
}
當(dāng)前文章:java一段抽獎代碼 java抽獎代碼思路
文章網(wǎng)址:http://www.rwnh.cn/article10/ddcpsdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、虛擬主機、網(wǎng)站設(shè)計、營銷型網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)