導航:首頁 > 編程語言 > java隨機產生6個數字

java隨機產生6個數字

發布時間:2023-03-14 10:06:47

java程序中怎樣生成0到9的6個隨機數,謝謝啦!要完整的程序,謝謝~~

public class MyRandom {
static Random r = new Random();
static String ssource = "0123456789";
static char[] src = ssource.toCharArray();
//產生隨機字元串
private static String randString (int length)
{
char[] buf = new char[length];
int rnd;
for(int i=0;i<length;i++)
{
rnd = Math.abs(r.nextInt()) % src.length;

buf[i] = src[rnd];
}
return new String(buf);
}

//調用該方法,產生隨機字元串,
//參數i: 為字元串的長度
public static String runVerifyCode(int i)
{
String VerifyCode = randString(i);
return VerifyCode;
}

public static void main(String[] args) {
MyRandom t=new MyRandom();
t.runVerifyCode(10);
}
}
在生成隨機數的地方直接調用上面的 MyRandom.runVerifyCode(int i)
;i是你需要生成幾位隨機數,

㈡ 用java編寫隨機生成一個6位的正整數,編程實現由這六位數字生成的最大數和最小數

publicstaticvoidmain(String[]args){
//根據數組個數循環
int[]ary=newint[6];
Stringresult="";
intmax=0;
intmin=9;

for(inti=0;i<6;i++){
ary[i]=(int)(Math.random()*10);
//首位數字不能為0
while(ary[0]==0){
ary[0]=(int)(Math.random()*10);
}
result+=ary[i];
//取出最大最小值,用於組裝最大最小值
max=Math.max(ary[i],max);
min=Math.min(ary[i],min);
}
//輸出隨機數字
System.out.println(Integer.parseInt(result));
//排序
Arrays.sort(ary);
//最大值
StringmaxResult="";
for(inti=5;i>=0;i--){
maxResult+=ary[i];
}
System.out.println(Integer.parseInt(maxResult));
//最小值
StringminResult="";
for(inti:ary){
minResult+=i;
}
System.out.println(Integer.parseInt(minResult));
}

㈢ 請問:java產生6個數字的隨機數怎麼寫

這個是我寫的一個產生-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) |

㈣ 關於Java里產生1-6隨機數的方法

用java.util.Random
類比較好用

Random
r=new
Random();

int
i=r.nextInt(6)+1;

生成1到6的數字應該機率比較相等;

(int)(Math.random()*10)%6+1產生0到9內的整數再
得到專1,2,3,4的概率大些屬

因為(int)(Math.random()*10)%得到的數是從0到9;0%6+1=1.。。。。。。5%6+1=6

……6%6+1=1.。。。。9%6+1=4;

所以不相等

另一種
(int)(Math.random()*6)+1

從0.1到0.9
*6得到數是int型是0,1,1,2,3,3,4,4,5,

+

㈤ java 列印六個不重復的隨機數。

sum=(int)(Math.random()*33 + 1) 就是取一個int值 在1-33之間! 第一次的時候 a[0]-a[5] 都是0所以那個for一定不會進! 從第二次開始 a[0]-a[5]就開始有值了, 那麼就拿sum值 用for循環 去和a[0]-a[5]依次進行比較, 如果發現有想同的,說明隨機數重復了, 那麼這一次尋找就不算數,就要進行i--的操作 同時 將標志值設為0,不入到a[]數組中~ 就這個意思了!

㈥ java 隨機生成一個六位數

publicclassGuessNum{
publicstaticvoidmain(String[]args){
inti=getRandomNum();
System.out.println("已生成隨機數!");
System.out.println(i);//方便測試列印出隨機數~~可刪除~~
while(true){
try{
System.out.print("請輸入一個6位正"+"整數:");
Readerreader=newInputStreamReader(System.in);
char[]b=newchar[6];
reader.read(b);
StringguessStr=newString(b).trim();
intguessInt=Integer.parseInt(guessStr);
if(guessInt==i){
System.out.println("恭喜猜對!");
break;
}
System.out.println("不對重猜!");
}catch(Exceptione){
System.out.println("輸入有誤!");
}
}
}

privatestaticintgetRandomNum(){
Randomr=newRandom();
returnr.nextInt(900000)+100000;
}
}

㈦ 請問用java從1-33個整數中隨機抽取6個數字 且不重復 1-16隨機抽取一個數,給小球

完整代碼為:

public class Main {

public static void main(String[] args) {
int index = 1;
int[] redBalls = new int[6];

Random random = new Random();
boolean getMoreRed = true;
boolean getAgain;
System.out.println("開始抽取紅球!");
while (getMoreRed) {
getAgain = false;
int red = random.nextInt(36) + 1;
System.out.print("本次抽取到的紅球為:[" + red + "]!");
for (int i = 0; i < index; i++) {
if (redBalls[i] == red) {
System.out.print("重復抽取,將重新抽取紅球");
getAgain = true;
break;
}
}
System.out.println("");
if (getAgain){
continue;
}
redBalls[index - 1] = red;
index++;
getMoreRed = index < 7;
}
System.out.println("抽取到的紅球為:");
Arrays.sort(redBalls);
for (int redBall : redBalls) {
System.out.print(redBall + " ");
}

System.out.println(" 開始抽取藍球!");
System.out.println("本次抽取到的藍球為:[" + (random.nextInt(16) + 1) + "]!");
}
}

閱讀全文

與java隨機產生6個數字相關的資料

熱點內容
iphone6s單手模式 瀏覽:79
vivo怎麼找刪除的app軟體 瀏覽:852
360裝機大師怎麼用教程 瀏覽:168
高一編程語言是什麼 瀏覽:421
phpword插入圖片 瀏覽:261
數控編程s300什麼意思 瀏覽:871
linuxab壓力測試 瀏覽:818
編程語言為什麼是c 瀏覽:797
悅me只能網關密碼錯誤 瀏覽:844
三星交集工具 瀏覽:939
資料庫中怎麼復製表結構 瀏覽:417
戴爾win10平板裝系統嗎 瀏覽:816
編程的變數名有哪些 瀏覽:124
360版本海島奇兵下載 瀏覽:370
常州ug數控編程培訓哪個學校好 瀏覽:802
資料庫的不等於怎麼寫 瀏覽:664
qq關閉送禮物動畫 瀏覽:128
京東健康碼在哪個文件夾里 瀏覽:891
數據線黑了怎麼消除 瀏覽:883
iphone6快捷鎖屏 瀏覽:55

友情鏈接