我对何时使用public static void xxx()
和感到困惑public void xxx()
。我得到的错误是在主类中。
生成随机数();
错误:(错误:无法从静态上下文引用非静态方法 generateRandomNumber())
获取用户输入();
他错误:(错误:无法从静态上下文引用非静态方法 getUserInput())
得到结果();
错误:(错误:无法从静态上下文引用非静态方法 getResult())
public class HighLowGame {
int randomNumber;
int guess;
public void generateRandomNumber(){
randomNumber = (0+(int)(Math.random() * ((0 - 99) + 1)));
}
public void getUserInput(){
guess = Integer.parseInt(JOptionPane.showInputDialog(
null,
"Plaese input"));
}
public String getResult(){
if(randomNumber<guess){
return "Your number is bigger than magic number";
}
else if (randomNumber>guess) {
return "Your number is smaller than magic number";
}
else {
return "You are correct! "+randomNumber;
}
}
public static void main(String[] args){
generateRandomNumber();
getUserInput();
getResult();
}
}