由于你沒有指定這10個(gè)整數(shù)的生成范圍,所以我這里假定是0~99之間的整數(shù),這樣用戶輸入時(shí)有10%的幾率命中。

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供楚雄州網(wǎng)站建設(shè)、楚雄州做網(wǎng)站、楚雄州網(wǎng)站設(shè)計(jì)、楚雄州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、楚雄州企業(yè)網(wǎng)站模板建站服務(wù),10余年楚雄州做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
整體代碼為:
public class Main {
public static void main(String[] args) {
//聲明長(zhǎng)度為10的隨機(jī)數(shù)數(shù)組
int[] randoms = new int[10];
Random random = new Random();
for (int i = 0; i 10; i++) {
//獲取0~99之間的一個(gè)隨機(jī)整數(shù),可通過調(diào)整nextInt的參數(shù)來修改隨機(jī)數(shù)范圍
int num = random.nextInt(100);
//如果新生成的數(shù)字已經(jīng)存在于隨機(jī)數(shù)數(shù)組中,則重新生成
if (checkDistinct(randoms, num)) {
i--;
continue;
}
randoms[i] = num;
}
//增序排序,好看
Arrays.sort(randoms);
System.out.println("請(qǐng)輸入一個(gè)整數(shù):");
Scanner scanner = new Scanner(System.in);
//嚴(yán)謹(jǐn)一點(diǎn)這里其實(shí)可以對(duì)輸入的in進(jìn)行校驗(yàn),檢驗(yàn)其是不是整數(shù),校驗(yàn)方法很多搜一下就有我這就不校驗(yàn)了
int in = scanner.nextInt();
System.out.println("生成的隨機(jī)數(shù)數(shù)組為:");
System.out.println(Arrays.toString(randoms));
if (checkDistinct(randoms, in)) {
System.out.println("輸入的數(shù)字[" + in + "]在其中");
} else {
System.out.println("輸入的數(shù)字[" + in + "]不在其中");
}
}
//檢查新生成的數(shù)字是否存在于隨機(jī)數(shù)數(shù)組中,若存在,返回true
private static boolean checkDistinct(int[] randoms, int num) {
for (int i = 0; i randoms.length; i++) {
if (randoms[i] == num) {
return true;
}
}
return false;
}
}
運(yùn)行結(jié)果:
輸入的數(shù)字存在時(shí):
輸入的數(shù)字不存在時(shí):
方法一代碼:
import java.util.Random;
public class Test {
public static void main(String[] args){
Random rand = new Random();
for(int i=0; i10; i++) {
System.out.println(rand.nextInt(100) + 1);
}
}
}
方法二代碼:
package edu.sjtu.erplab.io;
import java.util.Random;
public class RandomTest {
public static void main(String[] args) {
int max=20;
int min=10;
Random random = new Random();
int s = random.nextInt(max)%(max-min+1) + min;
System.out.println(s);
}
}
擴(kuò)展資料:
在windows下編譯java文件、執(zhí)行:
1、先創(chuàng)建一個(gè)txt,更改為test.java。
2、編寫代碼,為輸出為holloword。
3、找到cmd,并進(jìn)行打開cmd。
4、編譯java文件,輸入命令為javac test.java。
5、如果沒有報(bào)錯(cuò),查看當(dāng)前目錄下是否有class文件產(chǎn)生。
6、執(zhí)行class文件,在命令輸入java test,輸出為holloword。
可以先通過random方法生成一個(gè)隨機(jī)數(shù),然后將結(jié)果乘以100,然后再利用int方法進(jìn)行轉(zhuǎn)換,就可以得到一個(gè)0到99的隨機(jī)整數(shù)。
java代碼方法一如下:
int random=(int)(Math.random()*10+1)
java代碼方法二如下:
package bdqn_Empy;
import java.util.Random;
public class Text {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=0;
Random b=new Random();
a=b.nextInt(101);
System.out.println(a);
}
}
擴(kuò)展資料:
可以先通過 random方法生成一個(gè)隨機(jī)數(shù),然后將結(jié)果乘以10。此時(shí)產(chǎn)生的隨機(jī)數(shù)字即為大于等于0小于10的數(shù)字。
然后再利用nt方法進(jìn)行轉(zhuǎn)換它會(huì)去掉小數(shù)掉后面的數(shù)字即只獲取整數(shù)部分,不是四舍五入)。最后即可獲取一個(gè)0到9的整數(shù)型隨機(jī)數(shù)字。其實(shí)現(xiàn)方法很簡(jiǎn)單,就是對(duì)原有的 grandom方法按照如下的格式進(jìn)行變型:(int( Math. Random0*10)即可。
其實(shí)我們還可以對(duì)這個(gè)方法進(jìn)行擴(kuò)展,讓其產(chǎn)生任意范圍內(nèi)的隨機(jī)數(shù)。至需要將這個(gè)10換成n即可,如改為( int(Math. Random0n)。此時(shí)應(yīng)用程序就會(huì)產(chǎn)生一個(gè)大于等于0小與n之間的隨機(jī)數(shù)。
如將n設(shè)置為5,那么其就會(huì)產(chǎn)生一個(gè)0到5之間的整數(shù)型的隨機(jī)數(shù)。如果將這個(gè)寫成一個(gè)帶參數(shù)的方法,那么只要用戶輸入需要生成隨機(jī)數(shù)的最大值,就可以讓這個(gè)方法來生成制定范圍的隨機(jī)數(shù)。
完整代碼為:
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("本次抽取到的紅球?yàn)椋篬" + red + "]!");
for (int i = 0; i index; i++) {
if (redBalls[i] == red) {
System.out.print("重復(fù)抽取,將重新抽取紅球");
getAgain = true;
break;
}
}
System.out.println("");
if (getAgain){
continue;
}
redBalls[index - 1] = red;
index++;
getMoreRed = index 7;
}
System.out.println("抽取到的紅球?yàn)椋?);
Arrays.sort(redBalls);
for (int redBall : redBalls) {
System.out.print(redBall + " ");
}
System.out.println("\n\n開始抽取藍(lán)球!");
System.out.println("本次抽取到的藍(lán)球?yàn)椋篬" + (random.nextInt(16) + 1) + "]!");
}
}
運(yùn)行結(jié)果:
普通抽取:
重復(fù)時(shí)抽取:
Math.random()是令系統(tǒng)隨機(jī)選取大于等于 0.0 且小于 1.0 的偽隨機(jī) double 值,是Java語言常用代碼。
例如:var a:Number=Math.random()*2+1,設(shè)置一個(gè)隨機(jī)1到3的變量。
當(dāng)前名稱:java中隨機(jī)數(shù)字代碼,Java隨機(jī)數(shù)代碼
網(wǎng)頁URL:http://chinadenli.net/article31/dsedjpd.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站收錄、關(guān)鍵詞優(yōu)化、網(wǎng)站改版、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)