请问一个产生随机数的java程序怎么写?

来源:百度知道 编辑:UC知道 时间:2024/05/26 12:57:31
就是有一个按钮开始,然后不断产生一些随机数,然后点击按钮它停下来显示产生的最后一个随机数
最好给具体的代码哦

这个是我写的一个产生0-100的随机数的程序,

当然数的范围你可以自己定 Math.round(Math.random()*100),后面这个100你可以改成你自己想要的数

import javax.swing.*;
import java.awt.event.*;
public class RandomUsage extends JFrame
implements ActionListener
{
JButton bt=new JButton("随机数");
JLabel jt=new JLabel();
public RandomUsage()
{
this.setTitle("产生随机数");
this.setBounds(100,100,300,150);
this.setLayout(null);
this.add(bt);
bt.addActionListener(this);
bt.setBounds(20,20,80,50);
this.add(jt);
jt.setBounds(120,20,80,50);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt)
{
jt.setText(String.valueOf(Math.round(Math.random()*100)));
}
}
public static void main(String args[])
{
new RandomUsage();
}
}