java程序编写:使用Math类提供的随机数生成方法,产生一个1~100的随机整数并输出。
答案:3 悬赏:70 手机版
解决时间 2021-02-03 03:25
- 提问者网友:活着好累
- 2021-02-02 02:30
要可以运行出来的,拜托啦
最佳答案
- 五星知识达人网友:大漠
- 2021-02-02 03:59
这个是我写的一个产生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();
}
}
当然数的范围你可以自己定 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楼网友:孤老序
- 2021-02-02 06:07
public class kn02 {
public static void main(String[] args)
{
int num=(int)(Math.random()*100);
if(num<101&&num>0)
System.out.print(num);
}
}
- 2楼网友:舍身薄凉客
- 2021-02-02 05:20
public class MathTest {
public static void main(String[] args) {
int i= (int)(Math.random()*100);
System.out.println(i);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯