我正在做一个数字猜测程序。生成一个随机数,用户尝试猜测它。程序会打印“太高”或“太低”,让用户再次猜测。我在将第一个猜测输入到进行猜测的方法中时遇到问题。
这是我的课:
import java.util.Scanner;
public class Lab8
{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a number: ");
MyNumberGuess MyNumberGuess = new MyNumberGuess(in.nextInt());
while (MyNumberGuess.tooLow() == true || MyNumberGuess.tooHigh() == true)
{
if (MyNumberGuess.tooHigh() == true)
{
System.out.println("Too high");
System.out.println("Enter a number: ");
MyNumberGuess.MyNumberGuess(in.nextInt());
}
else if (MyNumberGuess.tooLow() == true)
{
System.out.println("Too low");
System.out.println("Enter a number: ");
MyNumberGuess.MyNumberGuess(in.nextInt());
}
}
System.out.println("Correct");
System.out.println("You made " + MyNumberGuess.getNumGuesses() + " guesses");
}
}
这是另一个类和问题方法:
import java.util.*;
public class MyNumberGuess
{
public static final int MAX_GUESS = 1000;
private int theNumber, numGuesses, prevGuess;
public MyNumberGuess(int inGuess)
{
Random generator = new Random();
numGuesses = 1;
prevGuess = inGuess;
theNumber = generator.nextInt(MAX_GUESS);
}
}
照原样,在这一行的第一堂课中编译时出现“找不到符号”错误:
MyNumberGuess.MyNumberGuess(in.nextInt());
我尝试以不同的方式调用它,使用参数而不是参数,并尝试单独调用变量,认为它们应该是私有的。任何帮助表示赞赏。