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年至今,是专业互联网技术服务公司,拥有项目成都做网站、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元轮台做网站,已为上家服务,为轮台各地企业和个人服务,联系电话:028-86922220
回答于 2022-11-16
现在的可以了,你试下
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.ArrayList;
import java.util.List;
public class Random {
public static void main(String[] args) {
String[] tiMu = new String[20];
for (int i = 0; i tiMu.length; i++) {
tiMu[i] = "第" + (i + 1) + "题";
}
String[] temp = new String[10];
//开始抽取题目
//产生10个随机数
ListInteger list = new ArrayListInteger();
int i;
while(list.size() 10){
i = (int) (Math.random() * 20);
if(!list.contains(i)){
list.add(i);
}
}
for (int j = 0; j list.size(); j++) {
temp[j] = tiMu[list.get(j)];
}
for (int iloop = 0; iloop temp.length; iloop++) {
System.out.print(temp[iloop] + " ");
}
}
}
代码已经写完了,亲测可用,望采纳哦!
我的一个简单思路如下:
(1)创建一个集合,并初始化一些数据。
(2)产生随机数,这个随机数是在0-集合大小之间的一个位置。
(3)根据随机产生的位置,输出集合元素。
代码实现:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Demo {
public static void main(String[] args) {
//创建一个集合
ListInteger list = new ArrayListInteger();
//初始化集合
for(int i=1;i=10;i++) {
list.add(i);
}
//产生随机数,范围在0~list.size之间
double index = Math.random()*10;
System.out.println("随机输出的数是:"+list.get((int)index));
}
}
结果截图:
完整代码为:
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) + "]!");
}
}
运行结果:
普通抽取:
重复时抽取: