導航:首頁 > 編程語言 > java10以內的隨機數

java10以內的隨機數

發布時間:2022-09-17 19:31:42

A. 在java中隨機生成10個整數,用戶求輸入一個數,判斷是否存在於這10個整數中

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

整體代碼為:

public class Main {

public static void main(String[] args) {
//聲明長度為10的隨機數數組
int[] randoms = new int[10];
Random random = new Random();
for (int i = 0; i < 10; i++) {
//獲取0~99之間的一個隨機整數,可通過調整nextInt的參數來修改隨機數范圍
int num = random.nextInt(100);
//如果新生成的數字已經存在於隨機數數組中,則重新生成
if (checkDistinct(randoms, num)) {
i--;
continue;
}
randoms[i] = num;
}
//增序排序,好看
Arrays.sort(randoms);

System.out.println("請輸入一個整數:");
Scanner scanner = new Scanner(System.in);
//嚴謹一點這里其實可以對輸入的in進行校驗,檢驗其是不是整數,校驗方法很多搜一下就有我這就不校驗了
int in = scanner.nextInt();
System.out.println("生成的隨機數數組為:");
System.out.println(Arrays.toString(randoms));
if (checkDistinct(randoms, in)) {
System.out.println("輸入的數字[" + in + "]在其中");
} else {
System.out.println("輸入的數字[" + in + "]不在其中");
}
}

//檢查新生成的數字是否存在於隨機數數組中,若存在,返回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;
}
}

B. 誰知道怎樣用Java產生1至10的隨機數並對它實現有序排列

用java的Random吧,
Random r=new Random();
int ran=r.nextInt(10);
這樣產生的就是1到10的數字。你用數組存起來排序就可以了
http://..com/question/87159986.html

C. java 十位 隨機數

使用隨機數產生類Random來產生:
public static long suiji = new Random().nextLong();

或者函數Math.random();可以產生一個0.0~1.0之間的隨機數

D. 求各位java的高手們 幫我設計一下產生10以內的隨機數a和b 並計算其減法和乘法

寫個偽代碼給你吧
double a = Math.random()*10;
double b = Math.random()*10;
然後計算就可以了
double c =a -b;
double d = a*b;

E. java生成5到10之間的隨機數

new Random().nextInt(6) + 5;

new Random().nextInt(6)這個生成的是0-5 所以加上5就是你要的啦。

F. 在Java 中怎樣產生1~10的隨機數

用這個就行了:

G. 用java:產生10個10-20的整型隨機數存入數組,然後求出最大值、最小值以及它們在數組中的位置

importjava.util.Arrays;

publicclassTestMain{
privatestaticfinalintsize=10;
publicstaticvoidmain(Stringargs[])throwsException{
intsize=10;
inta[]=newint[size];
intmax=10,min=20;
intmaxIndex=0,minIndex=0;
for(inti=0;i<size;i++){
//產生一個10到20的隨機整數
a[i]=(int)(Math.random()*11)+10;
if(a[i]>max){
max=a[i];
maxIndex=i+1;
}
if(a[i]<min){
min=a[i];
minIndex=i+1;
}
}
System.out.println(Arrays.toString(a));
System.out.println("最大值是:"+max+"下標是:"+maxIndex);
System.out.println("最小值是:"+min+"下標是:"+minIndex);
}
}

H. java如何獲取10到20隨機數

如下,寫快了,在Math前面加個強制轉換 int i=(int)Math:

int i=Math.round(Math.random()*(20-10)+10);

I. 怎麼使用java編程:隨機產生一個1-10之間愛的隨機數,由用戶重復從鍵盤輸入值,直到猜中該隨機數為止

代碼如下:

importjava.util.Date;
importjava.util.Scanner;

publicclassGuessNumberGames{
staticlongnowstart;
publicstaticvoidmain(String[]args){
intnumber=(int)(Math.random()*10+1);
inttemp=number;
System.out.println("請猜一個10以內的數:");
GuessNumberGamesrt=newGuessNumberGames();
intin=rt.Sn();
nowstart=newDate().getTime();
Booleanyn=rt.Compare(temp,in);
while(!yn){
System.out.println("請重新輸入:");
intin1=rt.Sn();
yn=rt.Compare(temp,in1);
}
System.out.println("正確數字是:"+number);
}
publicBooleanCompare(inttemp,intnumber){
Booleanyesno=null;
if(temp==number){
System.out.println("猜對了!");
longnowend=newDate().getTime();
System.out.println("一共用時"+(nowend-nowstart)/1000+"秒");
yesno=true;
}elseif(number<temp){
System.out.println("猜小了");
yesno=false;
}elseif(number>temp){
System.out.println("猜大了");
yesno=false;
}
returnyesno;
}
publicintSn(){
Scannersc=newScanner(System.in);
intin=sc.nextInt();
returnin;
}
}

滿意請採納!

J. java中獲取10到20隨機數的方法是什麼

方法1
(數據類型)(最小值+Math.random()*(最大值-最小值+1))
例:
int i = (int)(10+Math.random()*(20-10+1))
從10到20的int型隨數
--------------------------------------------------------------------

閱讀全文

與java10以內的隨機數相關的資料

熱點內容
linux查看文件的行數 瀏覽:692
fpga約束文件如何自動生成 瀏覽:377
linux打開的文件數 瀏覽:973
win8修改公用網路 瀏覽:324
linuxlcrt 瀏覽:30
編程里res是什麼意思 瀏覽:616
dm80082版本固件 瀏覽:37
蘋果手機用什麼可以打開psd文件 瀏覽:627
服務號和小程序的區別 瀏覽:888
360手機助手qq文件位置 瀏覽:693
如何學習數碼編程 瀏覽:60
微信消息發布出去 瀏覽:377
windows文件名一個個消失 瀏覽:907
易語言設置文件所有者 瀏覽:197
iphone手機如何顯示wifi密碼 瀏覽:880
文山大數據 瀏覽:496
3dsrxtools怎麼升級 瀏覽:903
樂高的機器人編程是用什麼 瀏覽:542
neato怎麼升級固件 瀏覽:637
蘋果在qq上下載的文件在哪裡可以找到 瀏覽:576

友情鏈接