请问:java产生6个数字的随机数怎么写?

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/05 07:46:16

请问:java产生6个数字的随机数怎么写?

这个是我写的一个产生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();
}
}
评论(2) |