我似乎无法跳过while循环,因为我的代码不明白我输入了正确的数字。
import java.util.Scanner;
public class HelloWorld1{
static Scanner userInput = new Scanner(System.in);
static int randomNumbe
public static void main(String[] args){
System.out.println(randomNum());
int randomGuess = -1 ;
while(randomGuess != randomNum()){
System.out.println("Guess a number between 0 to 100");
randomGuess = userInput.nextInt();
}
System.out.println("Yes the random number is:" + randomGuess);
}
public static int randomNum(){
randomNumber = (int) (Math.random()*101);
return randomNumber;
}
}
答案 0 :(得分:0)
当你调用randomNum()时,你会得到另一个随机值。尝试:
int expected = randomNum();
System.out.println(expected);
int guess = -1;
while(guess != expected) {
...
}
答案 1 :(得分:-1)
package randomguess;
import java.util.Scanner;
public class RandomGuess {
/**
* @param args the command line arguments
*/
static int randomNumber = 0;
public static int getRandomInt() {
randomNumber = (int) (Math.random()*101);
return randomNumber;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int guessInt = sc.nextInt();
int randomInt = getRandomInt();
while(guessInt != randomInt) {
System.out.println("Better luck nextTime :D Random number was "+randomInt);
guessInt = sc.nextInt();
randomInt = getRandomInt();
}
System.out.println("Yeah! You are oracle!");
}
}
这可能是你想要的。如果是的话,请告诉我。