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、运行效果如图
指定了中奖号码
未指定中奖号码
未指定中奖号码
现在的可以了,你试下
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()
}
}
}
}
}
就是孙谈从判指36个数中选出7个,这36个数是什么可以自己指定,但必须保证不重复,我写的一个代码则冲碰public
class
test
{
public
static
void
main(String[]
args)
{
int[]
a
=
new
int[36]
for(int
i
=
0
i
<
36
++i)
a[i]
=
i+1
for(int
i
=
0
i
<
5
++i)
randomSelect(a,
7)
}
static
void
randomSelect(int[]
a,
int
n)
{
randomShuffle(a,
7)
for(int
i
=
0
i
<
n
++i)
System.out.print(a[i]
+
"
")
System.out.println()
}
static
void
randomShuffle(int[]
a,
int
n)
{
for(int
i
=
0
i
<
n
++i)
{
int
r
=
(int)(Math.random()
*
a.length)
int
tmp
=
a[i]
a[i]
=
a[r]
a[r]
=
tmp
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)