摇号抽奖java代码 摇号程序代码
java语言实现一个摇号系统,但是可以内部设定中奖名单这个怎么实现?
1、简单控制台程序如下,如需界面需要自己加个。
创新互联公司自2013年创立以来,是专业互联网技术服务公司,拥有项目成都做网站、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元乐平做网站,已为上家服务,为乐平各地企业和个人服务,联系电话:18980820575
package zhidao;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
/**
* @author bufei
* @datetime 2020年8月31日15:54:11
*/
public class YaoHao {
public static void main(String[] args) {
String xian = "= = = = = = = = =";
// 起点 终点 奖个数 指定的号码个数
int start = 0, end = 0, prizeNum = 0, defaNum = 0;
System.out.println(xian);
System.out.println("欢迎使用xxx 抽奖系统!");
System.out.println(xian);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入号码范围例如 1 10:");
start = scanner.nextInt();
end = scanner.nextInt();
System.out.println("请输入奖项个数:");
prizeNum = scanner.nextInt();
System.out.println("请输入指定中奖号码个数,不指定请输入0:");
defaNum = scanner.nextInt();
int[] defa = new int[defaNum];
if (defaNum != 0) {
System.out.println("请输入指定的中奖号码,空格隔开:");
for (int i = 0; i defaNum; i++) {
defa[i] = scanner.nextInt();
}
}
System.out.println(xian);
System.out.println("本次抽奖中奖号码为:");
for (int num : randomDraw(start, end, prizeNum, defa)) {
System.out.print(num+" ");
}
}
/**
* @param start 抽奖范围起点
* @param end 抽奖号码范围终点
* @param prizeNum 中奖号码个数
* @param defa 指定中奖号码
* @return
*/
public static SetInteger randomDraw(int start, int end, int prizeNum, int[] defa) {
SetInteger set = new HashSet();
// 未指定中奖号码
if (defa.length == 0) {
//随机抽 prizeNum 个奖
while (set.size() prizeNum) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
} else {
//指定了中奖号码
//把指定的号码加入进去
for (int num : defa) {
set.add(num);
}
//如果没有全部指定 则继续抽剩余的奖项
while (set.size() prizeNum - defa.length) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
}
}
}
2、运行效果如图
指定了中奖号码
未指定中奖号码
未指定中奖号码
跪求:用Java设计抽奖程序
程序循环应该写在开始按钮的监听里,当按下开始按钮后,程序开始循环,当按停止按钮时,循环停止. 你可以用while循环, 条件是一个boolean型的值,当按开始值为true,当按停止值为false.
以JAVA为平台实现摇号抽奖
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.util.*;
import java.io.FileReader;
import java.io.File;
// 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();
mainPanel.setLayout(new GridLayout(1, 3, 5, 5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// Add the select and display panels to the main panel.
mainPanel.add(selectPanel);
mainPanel.add(displayPanel);
mainPanel.add(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\\JDK1.3.1\\bin\\images");
File[] files = dir.listFiles();
String imageName = null;
String temp = null;
int j = 0;
for (int i = 0; i files.length; i++) {
if (!files[i].isDirectory()) {
imageName = "images/" + files[i].getName();
}
temp = imageName.substring(imageName.lastIndexOf(".") + 1, imageName.length());
if(!temp.equals("gif"))
{
continue;
}
URL iconURL = ClassLoader.getSystemResource(imageName);
ImageIcon icon = new ImageIcon(iconURL);
images[j] = icon;
imageNames[j] = imageName.substring(7,imageName.lastIndexOf("."));
j++;
}
REAL_NUM_IMAGES = j;
// Create label for displaying moon phase images and put a border around
// it.
phaseIconLabel = new JLabel();
phaseIconLabel.setHorizontalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalAlignment(JLabel.CENTER);
phaseIconLabel.setVerticalTextPosition(JLabel.CENTER);
phaseIconLabel.setHorizontalTextPosition(JLabel.CENTER);
phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLoweredBevelBorder(), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
phaseIconLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(0, 0, 10, 0), phaseIconLabel
.getBorder()));
phaseResult = new JLabel();
// Create combo box with lunar phase choices.
phaseChoice = new JButton("开始/停止");
// Display the first image.
phaseIconLabel.setIcon(images[START_INDEX]);
phaseIconLabel.setText("");
// Add border around the select panel.
selectPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Select Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
resultPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Result Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
// Add border around the display panel.
displayPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory
.createTitledBorder("Display Phase"), BorderFactory
.createEmptyBorder(5, 5, 5, 5)));
// Add moon phases combo box to select panel and image label to
// displayPanel.
selectPanel.setLayout(new GridLayout(3,3));//这里原来是selectPanel.add(phaseChoice);
// displayPanel.add(phaseIconLabel);
// resultPanel.add(phaseResult);
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(phaseChoice);
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
selectPanel.add(new JLabel());
resultPanel.setLayout(new BorderLayout());
displayPanel.add(phaseIconLabel);
resultPanel.add(phaseResult);
// Listen to events from combo box.
phaseChoice.addActionListener(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)( Math.random()*REAL_NUM_IMAGES);
phaseIconLabel.setIcon(images[a]);
phaseResult.setText(imageNames[a]);
try{
Thread.sleep(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 {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
}
lunarPhasesFrame.setContentPane(phases.mainPanel);
// Exit when the window is closed.
lunarPhasesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Show the converter.
lunarPhasesFrame.pack();
lunarPhasesFrame.setVisible(true);
}
}
名称栏目:摇号抽奖java代码 摇号程序代码
本文URL:http://hbruida.cn/article/hhcdec.html