重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public class test {\x0d\x0a public static void main(String[] args) {\x0d\x0a //定义人名数组\x0d\x0a String [] name = {"张三","李四","王五","八神庵","不知火舞","大蛇","景天","唐雪见","李逍遥","赵灵儿"};\x0d\x0a//随机生成数组下标、\x0d\x0a int num = (int)(Math.random() * 1000);\x0d\x0a//对生成的随机数进行判断,如果小于数组下标,就跳出循环\x0d\x0awhile (numname.length-1) {\x0d\x0a if (num
成都创新互联成立于2013年,我们提供高端网站建设、成都网站制作、成都网站设计、网站定制、成都营销网站建设、小程序开发、微信公众号开发、成都网站推广服务,提供专业营销思路、内容策划、视觉设计、程序开发来完成项目落地,为三轮搅拌车企业提供源源不断的流量和订单咨询。
回答于 2022-11-16
完整代码为:
public class Main {
public static void main(String[] args) {
int index = 1;
int[] redBalls = new int[6];
Random random = new Random();
boolean getMoreRed = true;
boolean getAgain;
System.out.println("开始抽取红球!");
while (getMoreRed) {
getAgain = false;
int red = random.nextInt(36) + 1;
System.out.print("本次抽取到的红球为:[" + red + "]!");
for (int i = 0; i index; i++) {
if (redBalls[i] == red) {
System.out.print("重复抽取,将重新抽取红球");
getAgain = true;
break;
}
}
System.out.println("");
if (getAgain){
continue;
}
redBalls[index - 1] = red;
index++;
getMoreRed = index 7;
}
System.out.println("抽取到的红球为:");
Arrays.sort(redBalls);
for (int redBall : redBalls) {
System.out.print(redBall + " ");
}
System.out.println("\n\n开始抽取蓝球!");
System.out.println("本次抽取到的蓝球为:[" + (random.nextInt(16) + 1) + "]!");
}
}
运行结果:
普通抽取:
重复时抽取:
现在的可以了,你试下
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.Random;
public class Test {
public static void main(String[] args) {
int stuNums=30;
int[] nums=new int[stuNums];//存储学号的数组
boolean[] flags=new boolean[stuNums];//标记,用于标记对应下标的学号是否已经被抽取过了
for (int i = 0; i stuNums; i++) {
nums[i]=i+1;//给学号赋值
}
Random r=new Random();
while(true){
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("A等:"+nums[index]);
flags[index]=true; //标记已经被使用过了
break;
}
}
for (int i = 0; i 2; i++) {
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("B等:"+nums[index]);
flags[index]=true;
}else{
i--;//如果已经被抽取过了 ,那么i建议,再次循环
}
}
for (int i = 0; i 3; i++) {
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("c等:"+nums[index]);
flags[index]=true;
}else{
i--;
}
}
}
}
集合法
import java.util.ArrayList;
import java.util.Collections;
public class Test2 {
public static void main(String[] args) {
int stuNums=20;
ArrayListInteger list=new ArrayListInteger();
for (int i = 0; i stuNums; i++) {
list.add(i+1);
}
System.out.println("有序"+list);
Collections.shuffle(list);//打乱顺序
System.out.println("乱序"+list);
System.out.println("A等"+list.get(0));
System.out.println("B等"+list.get(1));
System.out.println("B等"+list.get(2));
System.out.println("C等"+list.get(3));
System.out.println("C等"+list.get(4));
System.out.println("C等"+list.get(5));
}
}