我寫(xiě)給你,如果是本專業(yè),以后要多多學(xué)習(xí)。JDK 1.6
創(chuàng)新互聯(lián)為您提適合企業(yè)的網(wǎng)站設(shè)計(jì)?讓您的網(wǎng)站在搜索引擎具有高度排名,讓您的網(wǎng)站具備超強(qiáng)的網(wǎng)絡(luò)競(jìng)爭(zhēng)力!結(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)頁(yè)設(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))+"號(hào)中了一等獎(jiǎng)";
output+="\n恭喜第"+(1+(int)(Math.random()*100))+"號(hào)"+(int)(1+(Math.random()*100))+"號(hào)"+"中了二等獎(jiǎng)";
for(int?i=0;i3;i++){
output+="\n恭喜第"+(1+(int)(Math.random()*100))+"號(hào)中了三等獎(jiǎng)";? ?
? ? }
JOptionPane.showMessageDialog(null,?output);
}
}
程序運(yùn)行結(jié)果截圖
中獎(jiǎng)的人是隨機(jī)的!
抽取問(wèn)題, 重點(diǎn)是 同一個(gè)學(xué)號(hào)不能重復(fù)被抽取.
解決辦法很多,
比如數(shù)組可以使用下標(biāo)來(lái)標(biāo)記,號(hào)碼是否被使用,使用了就繼續(xù)下一次抽取
也可以使用集合來(lái)抽取,把集合順序打亂,然后隨便抽幾個(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)被抽取過(guò)了
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)被使用過(guò)了
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)被抽取過(guò)了?,那么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("請(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ǔ)的 沒(méi)有異常判斷 無(wú)限循環(huán)輸入什么東西
import?java.util.Scanner;
/**
*
*/
public?class?f?{
public?static?void?main(String?args[]){
Scanner?scan?=?new?Scanner(System.in);
System.out.print("請(qǐng)輸入抽獎(jiǎng)號(hào)碼上限:");
int?max?=?scan.nextInt();
System.out.print("請(qǐng)輸入抽獎(jiǎng)次數(shù):");
int?n?=?scan.nextInt();
System.out.print("中獎(jiǎng)號(hào)碼依次為:");
for(int?i=0;in;i++){
System.out.print((int)(Math.random()*max+1)+"?");
}
}
}
生成100個(gè)對(duì)象,對(duì)象有個(gè)屬性,其中10個(gè)是大獎(jiǎng),40個(gè)是小獎(jiǎng),50個(gè)是無(wú)獎(jiǎng)。
放到一個(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(“無(wú)獎(jiǎng)“);
prizebeanList.add(prizeBean);
}
}
抽獎(jiǎng)代碼段:
/**
*獎(jiǎng)品池已經(jīng)空的,肯定返回?zé)o獎(jiǎng)了。。。
**/
if(prizebeanList.size()==0){
- 沒(méi)有中獎(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{
- 沒(méi)有中獎(jiǎng)哦,下次加油!
}.