java程序编写:使用Math类提供的随机数生成方法,产生一个1~100的随机整数并输出。

要可以运行出来的,拜托啦

这个是我写的一个产生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();
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-03
public class MathTest {
public static void main(String[] args) {
int i= (int)(Math.random()*100);
System.out.println(i);
}
}

本回答被提问者和网友采纳
第2个回答  2013-04-27
public class kn02 {
public static void main(String[] args)
{
int num=(int)(Math.random()*100);
if(num<101&&num>0)
System.out.print(num);
}
}
相似回答