新聞中心
用Java 編一個抽獎程序
用一個Int[]數(shù)組記錄隨機到的數(shù)字,

灌云網(wǎng)站建設公司成都創(chuàng)新互聯(lián)公司,灌云網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為灌云上千余家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設要多少錢,請找那個售后服務好的灌云做網(wǎng)站的公司定做!
插代碼:
int[] count=new i[6];//用于接收生成的隨機數(shù)
for(int i=0;ii.length;i++)
{
Random rand = new Random(); int c = rand.nextInt(); //int范圍類的隨機數(shù) c = rand.nextInt(30); //生成0-30以內的隨機數(shù) c = (int)(Math.random() * 30); //0-30以內的隨機數(shù) count[i]=c;}
java語言實現(xiàn)一個搖號系統(tǒng),但是可以內部設定中獎名單這個怎么實現(xiàn)?
1、簡單控制臺程序如下,如需界面需要自己加個。
package zhidao;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
/**
* @author bufei
* @datetime 2020年8月31日15:54:11
*/
public class YaoHao {
public static void main(String[] args) {
String xian = "= = = = = = = = =";
// 起點 ? ? ? ?終點 ? ? 獎個數(shù) ? ? ? ? ?指定的號碼個數(shù)
int start = 0, end = 0, prizeNum = 0, defaNum = 0;
System.out.println(xian);
System.out.println("歡迎使用xxx 抽獎系統(tǒng)!");
System.out.println(xian);
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入號碼范圍例如 1 10:");
start = scanner.nextInt();
end = scanner.nextInt();
System.out.println("請輸入獎項個數(shù):");
prizeNum = scanner.nextInt();
System.out.println("請輸入指定中獎號碼個數(shù),不指定請輸入0:");
defaNum = scanner.nextInt();
int[] defa = new int[defaNum];
if (defaNum != 0) {
System.out.println("請輸入指定的中獎號碼,空格隔開:");
for (int i = 0; i defaNum; i++) {
defa[i] = scanner.nextInt();
}
}
System.out.println(xian);
System.out.println("本次抽獎中獎號碼為:");
for (int num : randomDraw(start, end, prizeNum, defa)) {
System.out.print(num+" ");
}
}
/**
* @param start ? ?抽獎范圍起點
* @param end ? ? ?抽獎號碼范圍終點
* @param prizeNum 中獎號碼個數(shù)
* @param defa ? ? 指定中獎號碼
* @return
*/
public static SetInteger randomDraw(int start, int end, int prizeNum, int[] defa) {
SetInteger set = new HashSet();
// 未指定中獎號碼
if (defa.length == 0) {
//隨機抽 prizeNum 個獎
while (set.size() prizeNum) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
} else {
//指定了中獎號碼
//把指定的號碼加入進去
for (int num : defa) {
set.add(num);
}
//如果沒有全部指定 則繼續(xù)抽剩余的獎項
while (set.size() prizeNum - defa.length) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
}
}
}
2、運行效果如圖
指定了中獎號碼
未指定中獎號碼
未指定中獎號碼
商場推出幸運抽獎活動的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("謝謝參與!");
}
}
}
最基礎的 沒有異常判斷 無限循環(huán)輸入什么東西
求一份抽獎游戲(Java寫的代碼)
import?java.util.Scanner;
/**
*
*/
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)+"?");
}
}
}
Java代碼實現(xiàn)抽獎:從班級的學號中抽出一個一等獎,兩個二等獎,三個三等獎
抽取問題, 重點是 同一個學號不能重復被抽取.
解決辦法很多,
比如數(shù)組可以使用下標來標記,號碼是否被使用,使用了就繼續(xù)下一次抽取
也可以使用集合來抽取,把集合順序打亂,然后隨便抽幾個就可以了
參考代碼:數(shù)組法
import?java.util.Random;
public?class?Test?{
public?static?void?main(String[]?args)?{
int?stuNums=30;
int[]?nums=new?int[stuNums];//存儲學號的數(shù)組
boolean[]?flags=new?boolean[stuNums];//標記,用于標記對應下標的學號是否已經(jīng)被抽取過了
for?(int?i?=?0;?i??stuNums;?i++)?{
nums[i]=i+1;//給學號賦值
}
Random?r=new?Random();
while(true){
int?index?=?r.nextInt(stuNums);
if(!flags[index]){
System.out.println("A等:"+nums[index]);
flags[index]=true;?//標記已經(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));
}
}
當前文章:java搖數(shù)抽獎代碼 用java實現(xiàn)抽獎功能
URL標題:http://www.dlmjj.cn/article/hjopsc.html


咨詢
建站咨詢
