2016-04-28 141 views
-6

我的問題是,我需要生成一個隨機數,這是我已經完成該部分,但我需要使用該隨機數進行循環。循環隨機次數java

因此,例如,如果隨機數爲13,則循環一段代碼13次。

我有一個混亂,並沒有設法得到任何工作,並沒有能夠找到任何東西在線來幫助我。

基本上我試圖找出如果這甚至可能?

謝謝你們。

+1

我想你沒有足夠的搜索,你怎麼看待這個問題:http://www.tutorialspoint.com/java/java_for_loop.htm 。我們沒有被要求做你的功課。先做好你的事情,讓我們檢查你的代碼。 – gersonZaragocin

+1

[這是不可能!](https://reposti.com/i/m/bMm.jpg) – robotlos

回答

0

在這裏你去:

Random random = new Random(); 
int a = random.nextInt(); // As you said, you got this so far... 
while (a > 0) { // if you need your random number for something, just copy its value to new variable and place it here instead of a 
    // some code 
    a--; 

} 
1

這是很基本的Java。你有一個你隨機獲得的數字。只需在while循環或for循環中使用它。

int x = GetRandomNumber(); //You have stated you have already done this... 
for(int i = 0; i < x; i++){ 
//Do stuff here 
} 
2

這是一個簡單for循環:

for (int i = new Random().nextInt(); i > 0; i--) { 
    // do stuff 
}