import java.awt.BorderLayout
import java.awt.Color
import java.awt.Font
import java.awt.GridLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JTextField
import javax.swing.SwingConstants
public class RandomTest extends JFrame {
private static final long serialVersionUID = 6764423443555437772L
boolean isStop = false
private JLabel jl = new JLabel()
private JTextField jt = new JTextField()
private JButton start = new JButton("start")
private JButton stop = new JButton("stop")
private JPanel jp = new JPanel()
public RandomTest() {
this.jl.setHorizontalAlignment(SwingConstants.CENTER)
this.jl.setText("60"纤慎)// 设简启置随机数范围1到60
this.jl.setForeground(Color.red)// 设置字体颜色
this.jl.setFont(new Font("Dialog", 1, 24))// 设置字拦竖如体样式、大小
GridLayout layout = new GridLayout(1, 2)
this.add(jl, BorderLayout.NORTH)
this.add(jt, BorderLayout.CENTER)
this.jp.add(start, layout)
this.jp.add(stop, layout)
this.add(jp, BorderLayout.SOUTH)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)// 关闭窗口
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isStop = false
JTextThread jtt = new JTextThread(jt)
jtt.start()
}
})
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isStop = true
}
})
}
public static void main(String[] args) {
RandomTest rt = new RandomTest()
rt.setSize(400, 300)
rt.setLocationRelativeTo(null)// 设置窗口初始位置,屏幕中央
rt.setVisible(true)
}
class JTextThread extends Thread {
private JTextField jt
public JTextThread(JTextField jt) {
this.jt = jt
}
@Override
public void run() {
while (!isStop) {
try {
Thread.sleep(100)
int random = (int) (Math.random() * 60 + 1)
jt.setText(String.valueOf(random))
jt.setForeground(Color.blue)// 设置字体颜色
jt.setFont(new Font("Dialog", 1, 48))// 设置字体样式、大小
jt.setHorizontalAlignment(SwingConstants.CENTER)
} catch (InterruptedException e) {
e.printStackTrace()
}
}
}
}
}
import java.util.ArrayListimport java.util.List
孙逗 import java.util.Random
public class SelectNo {
public static void main(String[] args) {
//将1-35号码放入List集合
List<String> list = new ArrayList<String>()
for (int i = 1 i < 36 i++) {
list.add(""+i)
}
//新建一个List集合用来存放抽出来的号码
List<String> newList = new ArrayList<String>()
//随机抽取list集合中的一个元素,抽出后删除
Random rd = new Random()
int count = list.size()//集合中剩余号码
while (count > 0) {
int index = rd.nextInt(count)//抽出的号码的位置
//放入新的集合
newList.add(list.get(index))
//删除原有集合岁册的元素
list.remove(index)
count--//原有集合少了一个
}
//输出则雀卖号码
for (int i = 0 i < newList.size() i++) {
System.out.print(newList.get(i)+"\t")
//每五个换行
if(i%5 == 4){
System.out.println("")
}
}
}
}
1、简单控制台程序如下,如需界面需要自己加个。
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 = 0i <defaNumi++) {
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 Set<Integer>randomDraw(int start, int end, int prizeNum, int[] defa) {
Set<Integer>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、运行效果如图
指定了中奖号码
未指定中奖号码
未指定中奖号码
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)