package ch07;
import javaxswing;
public class Test2 {
public static void main(String[] args) {
String output="";
output+="恭喜第"+(1+(int)(Mathrandom()100))+"号中了一等奖";
output+="\n恭喜第"+(1+(int)(Mathrandom()100))+"号"+(int)(1+(Mathrandom()100))+"号"+"中了二等奖";
for(int i=0;i<3;i++){
output+="\n恭喜第"+(1+(int)(Mathrandom()100))+"号中了三等奖";
}
JOptionPaneshowMessageDialog(null, output);
}
}
程序运行结果截图
中奖的人是随机的!
帮你做了一个,不知是否满意呢?
import javaappletApplet;
import javaawtButton;
import javaawtColor;
import javaawtGraphics;
import javaawtTextField;
import javaawteventActionEvent;
import javautilRandom;
import javautilVector;
public class Lottery extends Applet {
private static final long serialVersionUID = 1L;
int w,h;
Button ok,out,setup;
String[] msg;
TextField[] gaiLv;
TextField[] jiangPin;
int mx,ml;
int maxNum;
Random ran;
Vector<Integer> fist;
Vector<Integer> sec;
Vector<Integer> third;
Vector<Integer> lucky;
boolean lot=false;
boolean iserr=false;
boolean issetup=false;
String mesg="输入错误";
String priseMsg="继续努力!";
public void init(){
w=400;
h=220;
mx=20;
ml=40;
ran=new Random();
thissetSize(w, h);
thissetLayout(null);
ok=new Button("抽奖");
out=new Button("退出");
setup=new Button("确认设置");
msg=new String[4];
msg[0]="一等奖";
msg[1]="二等奖";
msg[2]="三等奖";
msg[3]="幸运奖";
gaiLv=new TextField[4];
jiangPin=new TextField[4];
for(int i=0;i<4;i++){
gaiLv[i]=new TextField("00"+(i+1));
thisadd(gaiLv[i]);
gaiLv[i]setBounds(mx+ml, 75+i26, 60, 18);
jiangPin[i]=new TextField();
thisadd(jiangPin[i]);
jiangPin[i]setBounds(mx+ml3, 75+i26, 80, 18);
}
thisadd(ok);
oksetBounds(260, 180, 60, 28);
okaddActionListener(new LotButtonAction(this));
thisadd(out);
outsetBounds(330, 180, 60, 28);
outaddActionListener(new LotButtonAction(this));
thisadd(setup);
setupsetBounds(110, 180, 80, 24);
setupaddActionListener(new LotButtonAction(this));
}
public void paint(Graphics g){
gsetColor(Colorwhite);
gfillRect(0, 0, thisgetWidth(), thisgetHeight());
gsetColor(new Color(230,255,230));
gfillRect(0, 0, w, 30);
gsetColor(ColorBLUE);
gdrawString("JAVA抽奖系统", 130, 20);
gsetColor(ColorORANGE);
gdrawRect(10, 40, 230, 170);
gsetColor(ColorBLACK);
gdrawString("设置", mx, 60);
gdrawString("概率", mx+ml, 60);
gdrawString("奖品", mx+ml3, 60);
for(int i=0;i<msglength;i++){
gsetColor(new Color(255-(i30),45,89));
gdrawString(msg[i], 20, 90+i26);
}
if(lot==true){
gsetColor(new Color(rannextInt(255),rannextInt(255),rannextInt(255)));
if(priseMsglength()<=7){
gdrawString(priseMsg, 260, 100);
}else{
gdrawString(priseMsgsubstring(0, 5), 260, 100);
gdrawString(priseMsgsubstring(5), 260, 120);
}
}
if(iserr==true){
gdrawString(mesg, 260, 100);
}
}
public void getLucky(){
float firu=1;
float secu=1;
float thiu=1;
float fouu=1;
float minu=1;
if(gaiLv[0]getText()trim()length()>1){
firu=FloatparseFloat(gaiLv[0]getText());
if(firu<=0||firu>=1){
iserr=true;
return;
}
if(firu<minu){
minu=firu;
}
}
if(gaiLv[1]getText()trim()length()>1){
secu=FloatparseFloat(gaiLv[1]getText());
if(secu<=0||secu>=1){
iserr=true;
return;
}
if(secu<minu){
minu=secu;
}
}
if(gaiLv[2]getText()trim()length()>1){
thiu=FloatparseFloat(gaiLv[2]getText());
if(thiu<=0||thiu>=1){
iserr=true;
return;
}
if(thiu<minu){
minu=thiu;
}
}
if(gaiLv[3]getText()trim()length()>1){
fouu=FloatparseFloat(gaiLv[3]getText());
if(fouu<=0||fouu>=1){
iserr=true;
return;
}
if(fouu<minu){
minu=fouu;
}
}
if(minu>=1||minu<=0){
iserr=true;
return;
}
float aNum=1/minu;
maxNum=(int)aNum;
int count=(int)(firu/minu);
if(firu!=1){
fist=getLotteryVec(maxNum,count);
}else{
fistremoveAllElements();
}
count=(int)(secu/minu);
if(secu!=1){
sec=getLotteryVec(maxNum,count);
}else{
secremoveAllElements();
}
count=(int)(thiu/minu);
if(thiu!=1){
third=getLotteryVec(maxNum,count);
}else{
thirdremoveAllElements();
}
count=(int)(fouu/minu);
if(fouu!=1){
lucky=getLotteryVec(maxNum,count);
}else{
luckyremoveAllElements();
}
issetup=true;
iserr=false;
}
protected Vector<Integer> getLotteryVec(int maxNum,int num){
Vector<Integer> result=new Vector<Integer>();
for(int i=0;i<num;i++){
resultadd(rannextInt(maxNum));
}
return result;
}
protected int getaNum(){
return rannextInt(maxNum);
}
public int isLucky(int pNum){
for(int i=0;i<fistsize();i++){
if(fistget(i)==pNum){
return 1;
}
}
for(int i=0;i<secsize();i++){
if(secget(i)==pNum){
return 2;
}
}
for(int i=0;i<thirdsize();i++){
if(thirdget(i)==pNum){
return 3;
}
}
for(int i=0;i<luckysize();i++){
if(luckyget(i)==pNum){
return 4;
}
}
return -1;
}
public void ButtonActionPerformed(ActionEvent e){
String acName=egetActionCommand();
if(acNameequals("抽奖")){
if(issetup==false){
priseMsg="请先设置参数!";
lot=true;
repaint();
lot=false;
return;
}
lot=true;
priseMsg=getResult(getaNum());
repaint();
// lot=false;
}else if(acNameequals("退出")){
thissetVisible(false);
thisstop();
thisdestroy();
Systemexit(0);
}else if(acNameequals("确认设置")){
lot=false;
getLucky();
repaint();
}
}
public String getResult(int num){
int resu=isLucky(num);
String result="";
switch(resu){
case -1:
result="继续努力!";
break;
case 1:
result="恭喜你!一等奖!\n获得"+jiangPin[0]getText();
break;
case 2:
result="恭喜你!二等奖!\n获得"+jiangPin[1]getText();
break;
case 3:
result="恭喜你!三等奖!\n获得"+jiangPin[2]getText();
break;
case 4:
result="恭喜你!幸运奖!\n获得"+jiangPin[3]getText();
break;
default:
result="继续努力!";
}
return result+num;
}
}
class LotButtonAction implements javaawteventActionListener{
Lottery su;
public LotButtonAction(Lottery bun){
thissu=bun;
}
@Override
public void actionPerformed(ActionEvent e) {
suButtonActionPerformed(e);
}
}
import javaawtGridLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javanetURL;
import javaxswingBorderFactory;
import javaxswingImageIcon;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingUIManager;
import javautil;
import javaioFileReader;
import javaioFile;
// This example demonstrates the use of JButton, JTextField
// and JLabel
public class LunarPhases implements ActionListener {
final static int NUM_IMAGES = 1000;
final static int START_INDEX = 0;
int REAL_NUM_IMAGES = 0;
ImageIcon[] images = new ImageIcon[NUM_IMAGES];
String[] imageNames = new String[NUM_IMAGES];
JPanel mainPanel, selectPanel, displayPanel, resultPanel;
JButton phaseChoice = null;
JLabel phaseIconLabel = null, phaseResult = null;
// Constructor
public LunarPhases() {
// Create the phase selection and display panels
selectPanel = new JPanel();
displayPanel = new JPanel();
resultPanel = new JPanel();
// Add various widgets to the sub panels
addWidgets();
// Create the main panel to contain the two sub panels
mainPanel = new JPanel();
mainPanelsetLayout(new GridLayout(1, 3, 5, 5));
mainPanelsetBorder(BorderFactorycreateEmptyBorder(5, 5, 5, 5));
// Add the select and display panels to the main panel
mainPaneladd(selectPanel);
mainPaneladd(displayPanel);
mainPaneladd(resultPanel);
}
// Create and the widgets to select and display the phases of the moon
private void addWidgets() {
// Get the images and put them into an array of ImageIcon
File dir = new File("C:\\Program Files\\JavaSoft\\JDK131\\bin\\images");
File[] files = dirlistFiles();
String imageName = null;
String temp = null;
int j = 0;
for (int i = 0; i < fileslength; i++) {
if (!files[i]isDirectory()) {
imageName = "images/" + files[i]getName();
}
temp = imageNamesubstring(imageNamelastIndexOf("") + 1, imageNamelength());
if(!tempequals("gif"))
{
continue;
}
URL iconURL = ClassLoadergetSystemResource(imageName);
ImageIcon icon = new ImageIcon(iconURL);
images[j] = icon;
imageNames[j] = imageNamesubstring(7,imageNamelastIndexOf(""));
j++;
}
REAL_NUM_IMAGES = j;
// Create label for displaying moon phase images and put a border around
// it
phaseIconLabel = new JLabel();
phaseIconLabelsetHorizontalAlignment(JLabelCENTER);
phaseIconLabelsetVerticalAlignment(JLabelCENTER);
phaseIconLabelsetVerticalTextPosition(JLabelCENTER);
phaseIconLabelsetHorizontalTextPosition(JLabelCENTER);
phaseIconLabelsetBorder(BorderFactorycreateCompoundBorder(
BorderFactorycreateLoweredBevelBorder(), BorderFactory
createEmptyBorder(5, 5, 5, 5)));
phaseIconLabelsetBorder(BorderFactorycreateCompoundBorder(
BorderFactorycreateEmptyBorder(0, 0, 10, 0), phaseIconLabel
getBorder()));
phaseResult = new JLabel();
// Create combo box with lunar phase choices
phaseChoice = new JButton("开始/停止");
// Display the first image
phaseIconLabelsetIcon(images[START_INDEX]);
phaseIconLabelsetText("");
// Add border around the select panel
selectPanelsetBorder(BorderFactorycreateCompoundBorder(BorderFactory
createTitledBorder("Select Phase"), BorderFactory
createEmptyBorder(5, 5, 5, 5)));
resultPanelsetBorder(BorderFactorycreateCompoundBorder(BorderFactory
createTitledBorder("Result Phase"), BorderFactory
createEmptyBorder(5, 5, 5, 5)));
// Add border around the display panel
displayPanelsetBorder(BorderFactorycreateCompoundBorder(BorderFactory
createTitledBorder("Display Phase"), BorderFactory
createEmptyBorder(5, 5, 5, 5)));
// Add moon phases combo box to select panel and image label to
// displayPanel
selectPanelsetLayout(new GridLayout(3,3));//这里原来是selectPaneladd(phaseChoice);
// displayPaneladd(phaseIconLabel);
// resultPaneladd(phaseResult);
selectPaneladd(new JLabel());
selectPaneladd(new JLabel());
selectPaneladd(new JLabel());
selectPaneladd(new JLabel());
selectPaneladd(phaseChoice);
selectPaneladd(new JLabel());
selectPaneladd(new JLabel());
selectPaneladd(new JLabel());
selectPaneladd(new JLabel());
resultPanelsetLayout(new BorderLayout());
displayPaneladd(phaseIconLabel);
resultPaneladd(phaseResult);
// Listen to events from combo box
phaseChoiceaddActionListener(this);
}
boolean run = false;
// Implementation of ActionListener interface
public void actionPerformed(ActionEvent event) {
if(run){
run = false;
}
else{
run = true;
new Thread(){
public void run(){
while(run){
int a =(int)( Mathrandom()REAL_NUM_IMAGES);
phaseIconLabelsetIcon(images[a]);
phaseResultsetText(imageNames[a]);
try{
Threadsleep(100);
}
catch(Exception e){}
}
}
}start();
}
}
// main method
public static void main(String[] args) {
// create a new instance of LunarPhases
LunarPhases phases = new LunarPhases();
// Create a frame and container for the panels
JFrame lunarPhasesFrame = new JFrame("Lunar Phases");
// Set the look and feel
try {
UIManagersetLookAndFeel(UIManager
getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
}
lunarPhasesFramesetContentPane(phasesmainPanel);
// Exit when the window is closed
lunarPhasesFramesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
// Show the converter
lunarPhasesFramepack();
lunarPhasesFramesetVisible(true);
}
}
程序循环应该写在开始按钮的监听里,当按下开始按钮后,程序开始循环,当按停止按钮时,循环停止 你可以用while循环, 条件是一个boolean型的值,当按开始值为true,当按停止值为false
你是要JAVA可视化的 还是控制台的啊?
控制台的话 你可以用循环来取中奖号,将其存入list或set里(最好是set,因为set本来就不会重复)。如果用list存就另外写一个循环方法,来挨个判定该元素是不是已经在list中存在了,如果存在,就让选号的再加选一次,如果不重复,就放到list里去。。。。OK
以上就是关于使用java语言编写一个抽奖系统全部的内容,包括:使用java语言编写一个抽奖系统、java 制作抽奖小程序、以JAVA为平台实现摇号抽奖等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)