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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请问:java产生6个数字的随机数怎么写?相关的知识,希望对你有一定的参考价值。

你看看这样可以不:

public class Test
public static void main(String args[])
int[] nums = new int[6];
for(int i = 0;i < nums.length;i++)
nums[i] = (int)(Math.random()*10);

System.out.print("产生的6位随机数为:" + nums[0]+nums[1]+nums[2]+nums[3]+nums[4]+nums[5]);

参考技术A 这个是我写的一个产生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) |本回答被提问者和网友采纳
参考技术B 法1:
int a[]=new int[6];
for(int i=0;i<a.length;i++)
a[i]=(int)Math.round(Math.random()*100); //*100这个100看你想要多少以内的随机数,如果不 //乘,会是0-1的随机数
System.out.println(a[i]);

法2:
int a[]=new int [6];
Random r=new Random();
for(int i=0;i<a.length;i++)
a[i]=r.nextInt(100); //如果里边没有100,数字完全随机
System.out.println(a[i]);
参考技术C 我写个js的,提示下:
100000~999999

Math.floor(Math.random()*1000000+(100000));

以上是关于请问:java产生6个数字的随机数怎么写?的主要内容,如果未能解决你的问题,请参考以下文章