重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
生成100個(gè)對(duì)象,對(duì)象有個(gè)屬性,其中10個(gè)是大獎(jiǎng),40個(gè)是小獎(jiǎng),50個(gè)是無獎(jiǎng)。
創(chuàng)新互聯(lián)是一家專業(yè)提供內(nèi)丘企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、HTML5建站、小程序制作等業(yè)務(wù)。10年已為內(nèi)丘眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
放到一個(gè)List里。
每次抽中的步驟
1、隨機(jī)生成0-List長(zhǎng)度之間的數(shù)值 ,去取List中的相應(yīng)對(duì)象,并移除這個(gè)對(duì)象。
代碼如下。:
獎(jiǎng)品對(duì)象類:
public class PrizeBean {
private String type;
public String getType() {
return eggType;
}
public void setType(String eggType) {
this.eggType = eggType;
}
}
獎(jiǎng)品池初始化代碼段:
{
List prizebeanList = new ArrayList();
for (int i = 0; i 10; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBean.setType(“大獎(jiǎng)“);
prizebeanList.add(prizeBean);
}
for (int i = 0; i 40; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBean.setType(“小獎(jiǎng)“);
prizebeanList.add(prizeBean);
}
for (int i = 0; i 50; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBean.setType(“無獎(jiǎng)“);
prizebeanList.add(prizeBean);
}
}
抽獎(jiǎng)代碼段:
/**
*獎(jiǎng)品池已經(jīng)空的,肯定返回?zé)o獎(jiǎng)了。。。
**/
if(prizebeanList.size()==0){
- 沒有中獎(jiǎng)哦,下次加油!
return;
}
/**
* 隨機(jī)生成,獎(jiǎng)品池中獎(jiǎng)品數(shù)量的數(shù)字。。取出獎(jiǎng)品池中的數(shù)字。。移除記錄。返回。。
*/
int resultnum = (int) (Math.random() * prizebeanList.size());
PrizeBean resultPrizeBean = prizebeanList.get(resultnum);
prizebeanList.remove(resultPrizeBean);
if(resultPrizeBean.getType() .eqauls("大獎(jiǎng)"){
- 恭喜,大獎(jiǎng)!
}else if(resultPrizeBean.getType() .eqauls("小獎(jiǎng)"){
- 運(yùn)氣不錯(cuò)哦,小獎(jiǎng)!
}else{
- 沒有中獎(jiǎng)哦,下次加油!
}.
抽取問題, 重點(diǎn)是 同一個(gè)學(xué)號(hào)不能重復(fù)被抽取.
解決辦法很多,
比如數(shù)組可以使用下標(biāo)來標(biāo)記,號(hào)碼是否被使用,使用了就繼續(xù)下一次抽取
也可以使用集合來抽取,把集合順序打亂,然后隨便抽幾個(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)被抽取過了
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)被使用過了
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));
}
}
import java.util.Scanner;
public class Choujiang {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//定義Scanner類1234
System.out.println("請(qǐng)輸入四位卡號(hào):");//輸入提示語
int cardNo = sc.nextInt();//接受輸入數(shù)據(jù)
int gewei = cardNo%10;//分解個(gè)位
int shiwei = cardNo/10%10;//分解十位
int baiwei = cardNo/100%10;//分解百位
int qianwei = cardNo/1000;//分解千位
int sum = gewei+shiwei+baiwei+qianwei;
System.out.println("會(huì)員卡號(hào)四位之和是:"+sum);
if(sum20){//判斷各位數(shù)和是否大于20
System.out.println("恭喜,你是幸運(yùn)客戶!");
}else{
System.out.println("謝謝惠顧!");
}
}
}
public class Lucky {
public static void main(String[] args){
System.out.println("請(qǐng)輸入您的4位會(huì)員卡號(hào):");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt(); //接收用戶從控制臺(tái)輸入的會(huì)員卡號(hào),并保存在會(huì)員卡號(hào)變量中
int a = number/1000; //千位
int b = number%1000/100; //百位
int c = number%100/10; //十位
int d = number%10; //個(gè)位
if((a+b+c+d)20){
System.out.println("恭喜中獎(jiǎng)!您是幸運(yùn)客戶");
}else{
System.out.println("謝謝參與!");
}
}
}
最基礎(chǔ)的 沒有異常判斷 無限循環(huán)輸入什么東西
用一個(gè)Int[]數(shù)組記錄隨機(jī)到的數(shù)字,
插代碼:
int[] count=new i[6];//用于接收生成的隨機(jī)數(shù)
for(int i=0;ii.length;i++)
{
Random rand = new Random(); int c = rand.nextInt(); //int范圍類的隨機(jī)數(shù) c = rand.nextInt(30); //生成0-30以內(nèi)的隨機(jī)數(shù) c = (int)(Math.random() * 30); //0-30以內(nèi)的隨機(jī)數(shù) count[i]=c;}
我給你個(gè)比較簡(jiǎn)單的,,但是需要按照你的要求進(jìn)行稍微的修改。。然后在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我行我素購(gòu)物管理系統(tǒng) 幸運(yùn)抽獎(jiǎng)\n");
do{
// 需要的話請(qǐng)把隨機(jī)數(shù)調(diào)整成你想要的范圍(我這個(gè)是為了測(cè)試方便寫的1
(~3的隨機(jī)數(shù),根據(jù)你的需要把下面的3換成你想要的數(shù)字就行了)
int num=(int)(Math.random()*3+1);
System.out.print("請(qǐng)輸入會(huì)員卡號(hào)(4位整數(shù)):");
custNo=input.nextInt();
//百位數(shù)與隨機(jī)數(shù)相同的為幸運(yùn)者
int bai=custNo/100%10;
while(i==1){
if(custNo=1000custNo=9999){
break;
}
else{
System.out.println("\n會(huì)員號(hào)碼輸入有誤,請(qǐng)重新輸入:");
custNo=input.nextInt();
continue;
}
}
if(bai==num){
showAward();
System.out.print("\n卡號(hào):"+custNo+"是幸運(yùn)客戶,獲得"+awardName);
}else{
System.out.print("\n卡號(hào):"+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("輸入有誤!請(qǐng)重新輸入:");
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="美的電飯鍋";
}
}