1、簡(jiǎn)單控制臺(tái)程序如下,如需界面需要自己加個(gè)。
創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來(lái),是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元樂(lè)平做網(wǎng)站,已為上家服務(wù),為樂(lè)平各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話: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 = "= = = = = = = = =";
// 起點(diǎn) ? ? ? ?終點(diǎn) ? ? 獎(jiǎng)個(gè)數(shù) ? ? ? ? ?指定的號(hào)碼個(gè)數(shù)
int start = 0, end = 0, prizeNum = 0, defaNum = 0;
System.out.println(xian);
System.out.println("歡迎使用xxx 抽獎(jiǎng)系統(tǒng)!");
System.out.println(xian);
Scanner scanner = new Scanner(System.in);
System.out.println("請(qǐng)輸入號(hào)碼范圍例如 1 10:");
start = scanner.nextInt();
end = scanner.nextInt();
System.out.println("請(qǐng)輸入獎(jiǎng)項(xiàng)個(gè)數(shù):");
prizeNum = scanner.nextInt();
System.out.println("請(qǐng)輸入指定中獎(jiǎng)號(hào)碼個(gè)數(shù),不指定請(qǐng)輸入0:");
defaNum = scanner.nextInt();
int[] defa = new int[defaNum];
if (defaNum != 0) {
System.out.println("請(qǐng)輸入指定的中獎(jiǎng)號(hào)碼,空格隔開(kāi):");
for (int i = 0; i defaNum; i++) {
defa[i] = scanner.nextInt();
}
}
System.out.println(xian);
System.out.println("本次抽獎(jiǎng)中獎(jiǎng)號(hào)碼為:");
for (int num : randomDraw(start, end, prizeNum, defa)) {
System.out.print(num+" ");
}
}
/**
* @param start ? ?抽獎(jiǎng)范圍起點(diǎn)
* @param end ? ? ?抽獎(jiǎng)號(hào)碼范圍終點(diǎn)
* @param prizeNum 中獎(jiǎng)號(hào)碼個(gè)數(shù)
* @param defa ? ? 指定中獎(jiǎng)號(hào)碼
* @return
*/
public static SetInteger randomDraw(int start, int end, int prizeNum, int[] defa) {
SetInteger set = new HashSet();
// 未指定中獎(jiǎng)號(hào)碼
if (defa.length == 0) {
//隨機(jī)抽 prizeNum 個(gè)獎(jiǎng)
while (set.size() prizeNum) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
} else {
//指定了中獎(jiǎng)號(hào)碼
//把指定的號(hào)碼加入進(jìn)去
for (int num : defa) {
set.add(num);
}
//如果沒(méi)有全部指定 則繼續(xù)抽剩余的獎(jiǎng)項(xiàng)
while (set.size() prizeNum - defa.length) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
}
}
}
2、運(yùn)行效果如圖
指定了中獎(jiǎng)號(hào)碼
未指定中獎(jiǎng)號(hào)碼
未指定中獎(jiǎng)號(hào)碼
程序循環(huán)應(yīng)該寫(xiě)在開(kāi)始按鈕的監(jiān)聽(tīng)里,當(dāng)按下開(kāi)始按鈕后,程序開(kāi)始循環(huán),當(dāng)按停止按鈕時(shí),循環(huán)停止. 你可以用while循環(huán), 條件是一個(gè)boolean型的值,當(dāng)按開(kāi)始值為true,當(dāng)按停止值為false.
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("開(kāi)始/停止");
// 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));//這里原來(lái)是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);
}
}