无法从另一个类 JAVA 调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19943801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't call a method from another class JAVA
提问by coinbird
I'm making a number guessing program. A random number is generated, and the user tries to guess it. The program will print "too high" or "too low" and let the user guess again. I'm having an issue inputing guesses after the first one into the method that takes the guess.
我正在制作一个猜数字程序。生成一个随机数,用户尝试猜测它。程序会打印“太高”或“太低”,让用户再次猜测。我在将第一个猜测输入到采用猜测的方法之后时遇到问题。
Here is my class:
这是我的课:
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");
}
}
Here's the other class, and the problem method:
这是另一个类,以及问题方法:
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);
}
}
As is, I'm getting a "cannot find symbol" error when compiling in my first class on this line:
照原样,在这一行的第一堂课中编译时出现“找不到符号”错误:
MyNumberGuess.MyNumberGuess(in.nextInt());
I've tried calling it in different ways, using parameters and not, and trying to call the variables alone, thought they are supposed to be private. Any help is appreciated.
我尝试以不同的方式调用它,使用参数而不是使用参数,并尝试单独调用变量,认为它们应该是私有的。任何帮助表示赞赏。
采纳答案by Sotirios Delimanolis
You previously used
您之前使用过
MyNumberGuess MyNumberGuess = new MyNumberGuess(in.nextInt());
to create an instance of your class. Why would you then use
创建类的实例。你为什么要使用
MyNumberGuess.MyNumberGuess(in.nextInt());
to do the same thing?
做同样的事情?
This
这个
public MyNumberGuess(int inGuess)
{
Random generator = new Random();
numGuesses = 1;
prevGuess = inGuess;
theNumber = generator.nextInt(MAX_GUESS);
}
is a constructor. You need to invoke it with the new
operator.
是一个构造函数。您需要使用new
运算符调用它。
Just re-initialize your variable
只需重新初始化您的变量
MyNumberGuess = new MyNumberGuess(in.nextInt());
Note that java convention states that variables' names should begin with a lower case character.
请注意,java 约定规定变量的名称应以小写字符开头。
On another note, this piece of code
另一方面,这段代码
while (MyNumberGuess.tooLow() == true || MyNumberGuess.tooHigh() == true)
is redundant. The method call MyNumberGuess.tooLow()
already returns a true
or false
value, so why compare it with == true
? Just use it directly. For example
是多余的。方法调用MyNumberGuess.tooLow()
已经返回 atrue
或false
值,那么为什么将它与== true
? 直接用就行了。例如
if (MyNumberGuess.tooLow()) // read it as "If my number guess is too low"
or
或者
if (!MyNumberGuess.tooLow()) // read it as "If my number guess is not too low"
Apply that appropriately with the while
.
将其适当地应用到while
.
回答by Paul Samsotha
Don't use the exact class name as the variable name
不要使用确切的类名作为变量名
MyNumberGuess MyNumberGuess = new MyNumberGuess(in.nextInt());
Change the casing if anything
有的话换外壳
MyNumberGuess myNumberGuess = new MyNumberGuess(in.nextInt());
回答by elvishimpersonators
You are missing a curly bracket, you have your class definition and then you have a function definition but no ending semicolon. And you shouldn't use the same name for so many variables.
你缺少一个大括号,你有你的类定义,然后你有一个函数定义但没有结束分号。并且您不应该对这么多变量使用相同的名称。
回答by Helzgate
I had a weird issue: no matter what I couldn't access the public methods of one of my classes whether it was static or whether I setup to instantiate it. I ended up deleting the class and creating a new one with a different name. I must have been stepping on a built in class name.
我有一个奇怪的问题:无论我无法访问我的一个类的公共方法,无论它是静态的还是我设置来实例化它。我最终删除了该类并创建了一个具有不同名称的新类。我一定是踩到了一个内置的类名。