
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 javautilScanner;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(Systemin);
int x=inputnextInt();
switch (x) {
case 1:
Systemoutprintln("一等奖");
break;
case 2:
Systemoutprintln("二等奖");
break;
case 3:
Systemoutprintln("三等奖");
break;
default:
break;
}
}
}
找了两组程序:
代码一:
import javaawt;
import javaawtevent;
import javaxswing;
class GoodLucky extends JFrame implements ActionListener
{
JTextField tf = new JTextField();
JButton b1 = new JButton("开始");
JButton b2 = new JButton("停止");
boolean sg=false;
public GoodLucky(){
b1setActionCommand("start");
JPanel p = new JPanel();
padd(b1);
padd(b2);
b1addActionListener(this);
b2addActionListener(this);
b2setEnabled(false);
thisgetContentPane()add(tf,"North");
thisgetContentPane()add(p,"South");
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thissetSize(300,200);
thissetLocation(300,300);
Cursor cu = new Cursor(CursorHAND_CURSOR);
thissetCursor(cu);
thissetVisible(true);
tfsetText("祝大家鸿运!");
thisgo();
}
public void go()
{
while(true)
{
if(sg==true)
{
String s = " ";
for(int j = 1; j <= 7;j++)
{
int i = (int)(Mathrandom() 9) + 1;
if(i < 10)
{
s = s + " 0" + i;
}
else
{
s = s + " " + i;
}
}
tfsetText(s);
}
try
{
Threadsleep(100);
}
catch(javalangInterruptedException e)
{
eprintStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
String s = egetActionCommand();
if(sequals("start"))
{
sg = true;
b1setEnabled(false);
b2setEnabled(true);
}
else
{
sg= false;
b2setEnabled(false);
b1setEnabled(true);
}
}
public static void main(String[] args)
{
new GoodLucky();
}
}
代码二:
package comsofteemlesson03;
import javaawtBorderLayout;
import javaawtContainer;
import javaawtFont;
import javaawteventMouseAdapter;
import javaawteventMouseEvent;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJTextField;
public class LuckyNumberInterface extends JFrame {
JLabel ltitle = new JLabel("欢迎使用本抽奖机", JLabelCENTER);
JLabel linput = new JLabel("幸运号码:");
JTextField input = new JTextField(20);
JButton btnStart = new JButton("开始");
JButton btnStop = new JButton("停止");
JPanel lpanel = new JPanel();
JPanel btnPanel = new JPanel();
public LuckyNumberInterface() {
super("抽奖机");
final LuckyNumber ln = new LuckyNumber(input);
lnstart();
Container container = getContentPane();
lpaneladd(linput);
lpaneladd(input);
btnPaneladd(btnStart);
btnPaneladd(btnStop);
Font font = new Font("宋体", FontBOLD, 16);
ltitlesetFont(font);
containeradd(ltitle, BorderLayoutNORTH);
containeradd(lpanel, BorderLayoutCENTER);
containeradd(btnPanel, BorderLayoutSOUTH);
btnStartaddMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
lnsetFlag(true);
}
});
btnStopaddMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
lnsetFlag(false);
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 120);//设置窗口的显示大小
setLocation(400, 300);//设置窗口显示的位置
setVisible(true);//显示窗口
}
public static void main(String[] args) {
new LuckyNumberInterface();
}
}
package comsofteemlesson03;
import javatextDecimalFormat;
import javautilRandom;
import javaxswingJTextField;
public class LuckyNumber extends Thread {
private String luckyNumber = "";
private boolean flag = false;//开关
private JTextField input;
public LuckyNumber(JTextField input) {
thisinput = input;
}
public void setFlag(boolean flag) {
thisflag = flag;
}
@Override
public void run() {
while (true) {
if (flag) {
generatorLuckyNumber();
}
}
}
public void generatorLuckyNumber() {
String[] firstNumber = { "130", "131", "134", "137", "135", "139",
"150", "151", "155", "158", "159" };
Random rd = new Random();
int number = rdnextInt(10000);
DecimalFormat df = new DecimalFormat("0000");
String lastNumber = dfformat(number);
luckyNumber = firstNumber[rdnextInt(firstNumberlength)] + ""
+ lastNumber;
inputsetText("");
inputsetText(luckyNumber);
}
}
以上就是关于使用java语言编写一个抽奖系统全部的内容,包括:使用java语言编写一个抽奖系统、Java编程 编写一程序 它运行都等待从键盘输入一个1到3的整数 然后输出一 二 三等奖 (SWITCH语句)、能帮忙用java做一个抽奖程序吗,就是简单的一,二,三等奖,特等奖。谢谢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)