我是java新手,我很接近答案。我希望我能得到一些提示,但不是完整的答案。我更愿意自己推导出逻辑。
/*
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);
}
我的代码有什么问题?我意识到虽然我将 i 固定在 1 到 50 之间,但它仍然超过了。