我是新來的java,我很接近answers.i希望我可以有一些提示,但不是完整的答案。我寧願將自己的邏輯推出。while循環中的隨機數
/*
Write a method named makeGuesses that will guess numbers
between 1 and 50 inclusive until it makes a guess of at least 48.
It should report each guess and at the end should report
the total number of guesses made. Below is a sample execution:
*/
guess = 43
guess = 47
guess = 45
guess = 27
guess = 49
total guesses = 5
public void makeGuesses(){
Scanner sc=new Scanner(System.in);
Random rnd=new Random();
int i=1
int counter=0;
while(i>=1 && i<=50){
i=rnd.nextInt();
System.out.println("guess = " + i);
counter++;
}
System.out.print("total guesses = "+ counter);
}
這有什麼錯我的代碼?我意識到雖然我解決我的我是1到50之間,但仍超過。
這將打印出0-50個隨機數。像'guess = 10,guess = 50,guess = 73'。那是你要的嗎?最後它會打印出循環的數量。 – christopher 2013-04-06 11:34:27
ya.That就是我想要的。但我得到的猜測= 1497208734.我認爲我在while循環中修復了它? – user2179615 2013-04-06 11:36:04
已嘗試rnd.nextInt(50)?這會限制隨機數的範圍 – diogod 2013-04-06 11:36:08