代碼如下:
創(chuàng)新互聯(lián)公司是專業(yè)的馬關(guān)網(wǎng)站建設(shè)公司,馬關(guān)接單;提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行馬關(guān)網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Luck extends JFrame implements ActionListener{
JTextField tf = new JTextField();
JButton b1 = new JButton("開始");
JButton b2 = new JButton("停止");
boolean isGo = false;
public Luck(){
b1.setActionCommand("start");
JPanel p = new JPanel();
p.add(b1);
p.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
b2.setEnabled(false);
this.getContentPane().add(tf,"North");
this.getContentPane().add(p,"South");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,200);
this.setLocation(300,300);
Cursor cu = new Cursor(Cursor.HAND_CURSOR);
this.setCursor(cu);
this.setVisible(true);
tf.setText("welcome you! ");
this.go();
}
public void go(){
while(true){
if(isGo == true){
String s = "";
for(int j = 1; j = 8;j++){
int i = (int)(Math.random() * 31) + 1;
if(i 10){
s = s + " 0" + i;
}else{
s = s + " " + i;
}
}
tf.setText(s);
}
try{
Thread.sleep(10);
}catch(java.lang.InterruptedException e){
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if(s.equals("start")){
isGo = true;
b1.setEnabled(false);
b2.setEnabled(true);
}else{
isGo = false;
b2.setEnabled(false);
b1.setEnabled(true);
}
}
public static void main(String[] args){
new Luck();
}
}
看下是你的要求不。若有疑問Hi我。
方案
代碼
public class NumberDemo{
public static void main(String []args) {
int[] num=new int[6];
int[] vis=new int[33];//vis[0]---vis[32]
// vis[i]:0 1 vis[5]=0表示5之前沒有出現(xiàn)過 vis[5]=1表示5之前出現(xiàn)過
int cnt = 0, tmp;
while(cnt 6) {
tmp ?= (int)(Math.random()*32 + 1);
if(vis[tmp] == 0) {
num[cnt++] = tmp;
vis[tmp] = 1;
}
}
System.out.println(Arrays.toString(num));
}
}
運(yùn)行結(jié)果:
抽取問題, 重點(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));
}
}
網(wǎng)站題目:關(guān)于java抽獎(jiǎng)代碼1到32的信息
當(dāng)前網(wǎng)址:http://muchs.cn/article12/ddcgpdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、電子商務(wù)、企業(yè)建站、ChatGPT、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)