我的 for 循环语句中的 RandomNumber 值发生了变化。这可以防止程序实际将生成的 RandomNumber 与用户输入的数字相匹配。如何修改我的 for 循环,使其不影响 RandomNumber?
这是我在 NetBeans 中编写的 java 代码;
package numberguess;
/**
*
* @author Marion
*/
public class NumberGuess {
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
int randNum = 0, guessNum = 0;
// Generates a random number from 1 to 10
randNum = new java.util.Random().nextInt(10) + 1;
System.out.println("I am thinking of a random number from 1 to 10");
for (randNum = 0; randNum < 3; randNum = randNum + 1) {
System.out.print("Guess?");
java.util.Scanner scan = new java.util.Scanner(System.in);
guessNum = scan.nextInt();
System.out.println("You guessed " + guessNum);
if (randNum == guessNum) {
System.out.println("You Guessed it!");
break;
}
}
}
}