我寫給你,如果是本專業(yè),以后要多多學(xué)習(xí)。JDK 1.6

創(chuàng)新互聯(lián)為您提適合企業(yè)的網(wǎng)站設(shè)計(jì)?讓您的網(wǎng)站在搜索引擎具有高度排名,讓您的網(wǎng)站具備超強(qiáng)的網(wǎng)絡(luò)競爭力!結(jié)合企業(yè)自身,進(jìn)行網(wǎng)站設(shè)計(jì)及把握,最后結(jié)合企業(yè)文化和具體宗旨等,才能創(chuàng)作出一份性化解決方案。從網(wǎng)站策劃到成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè), 我們的網(wǎng)頁設(shè)計(jì)師為您提供的解決方案。
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class LuckyDrawn {
private JFrame f = new JFrame("Lucky Drawn");
private JButton btn = new JButton("Drawn");
private JTextField result = new JTextField("Result");
private static final int MIN = 1;
private static final int MAX = 30;
private static final int RESULT_COUNT = 7;
public LuckyDrawn(){
f.add(btn);
f.add(result);
result.setEditable(false);
btn.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
Random rand = new Random();
SetInteger set = new TreeSetInteger();
while(set.size() RESULT_COUNT){
set.add(new Integer(rand.nextInt(MAX)+ 1));
}
StringBuffer sb = new StringBuffer("Lucky numbers are: ");
for (Iterator iter = set.iterator(); iter.hasNext();) {
sb.append(((Integer) iter.next()).intValue()).append(", ");
}
result.setText(sb.substring(0, sb.length() - 2).toString());
f.pack();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
f.setLayout(new GridLayout(2, 1));
f.setVisible(true);
f.setLocation(500, 200);
f.pack();
f.validate();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new LuckyDrawn();
}
}
package?ch07;
import?javax.swing.*;
public?class?Test2?{
public?static?void?main(String[]?args)?{
String?output="";
output+="恭喜第"+(1+(int)(Math.random()*100))+"號中了一等獎";
output+="\n恭喜第"+(1+(int)(Math.random()*100))+"號"+(int)(1+(Math.random()*100))+"號"+"中了二等獎";
for(int?i=0;i3;i++){
output+="\n恭喜第"+(1+(int)(Math.random()*100))+"號中了三等獎";? ?
? ? }
JOptionPane.showMessageDialog(null,?output);
}
}
程序運(yùn)行結(jié)果截圖
中獎的人是隨機(jī)的!
抽取問題, 重點(diǎn)是 同一個(gè)學(xué)號不能重復(fù)被抽取.
解決辦法很多,
比如數(shù)組可以使用下標(biāo)來標(biā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];//存儲學(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));
}
}
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; //個(gè)位
if((a+b+c+d)20){
System.out.println("恭喜中獎!您是幸運(yùn)客戶");
}else{
System.out.println("謝謝參與!");
}
}
}
最基礎(chǔ)的 沒有異常判斷 無限循環(huán)輸入什么東西
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)+"?");
}
}
}
生成100個(gè)對象,對象有個(gè)屬性,其中10個(gè)是大獎,40個(gè)是小獎,50個(gè)是無獎。
放到一個(gè)List里。
每次抽中的步驟
1、隨機(jī)生成0-List長度之間的數(shù)值 ,去取List中的相應(yīng)對象,并移除這個(gè)對象。
代碼如下。:
獎品對象類:
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;
}
/**
* 隨機(jī)生成,獎品池中獎品數(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("小獎"){
- 運(yùn)氣不錯(cuò)哦,小獎!
}else{
- 沒有中獎哦,下次加油!
}.
本文標(biāo)題:java抽獎軟件源代碼,java抽獎軟件源代碼是什么
文章分享:http://chinadenli.net/article22/hsgecc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、小程序開發(fā)、網(wǎng)站營銷、微信公眾號、全網(wǎng)營銷推廣、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)