java 找不到标志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/632370/
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
Cannot Find Symbol
提问by Patrick Cassell
I have created a small Java application to automatically test some expressions for a true/false condition.
我创建了一个小型 Java 应用程序来自动测试一些表达式的真/假条件。
I am getting two compiler errors in both jGRASP and with the javac command.
我在 jGRASP 和 javac 命令中都遇到了两个编译器错误。
The code follows:
代码如下:
public class MathTest {
public static void main(String[] args) {
int x = 10;
int y = 20;
int z = 30;
String string1 = "six";
String string2 = "six";
if (x < 10 || x > 10)
System.out.print("True");
else
System.out.print("False");
if (z - y == x && Math.abs(y - z) == z)
System.out.print("True");
else
System.out.print("False");
if (x < 10 && x < 10)
System.out.print("True");
else
System.out.print("False");
if (string1.equals(string2))
System.out.print("True");
else
System.out.print("False");
if (x > y || y > x)
System.out.print("True");
else
System.out.print("False");
if (!(x < y + z) || !(x + 10 <= 20))
System.out.print("True");
else
System.out.print("False");
if (string1 == string2)
System.out.print("True");
else
System.out.print("False");
}
}
The error message is:
错误信息是:
MathTest.java:14: cannot find symbol
symbol : method abs(int)
location: class Math
if(z - y == x && Math.abs(y - z) == z)
^
./Math.java:13: cannot find symbol
symbol : method abs(int)
location: class Math
if(z - y == x && Math.abs(y - z) == z)
^
2 errors
What am I doing wrong?
我究竟做错了什么?
In the unlikely event that my instructor or any administrator from Salt Lake Community College ever comes across this question, let me make my intentions clear. This question is posted in the greatest spirit of academic honesty. I ask this question to seek general advice and help in understanding the proper way to use the Java programming language. I in no way use the work of others and represent it as my own work. I use the answers provided here as a general aid in my understanding. I do all my own work and do not copy work provided by people answering my question.
万一我的导师或盐湖社区学院的任何管理员遇到这个问题,让我明确我的意图。这个问题是本着最大的学术诚信精神发布的。我问这个问题是为了寻求一般建议并帮助理解使用 Java 编程语言的正确方法。我绝不会使用他人的作品并将其呈现为我自己的作品。我使用这里提供的答案作为我理解的一般帮助。我做我自己的所有工作,不会复制回答我问题的人提供的工作。
回答by OscarRyz
You have a Math class, and you're attempting to use the abs() method.
您有一个 Math 类,并且您正在尝试使用 abs() 方法。
The question is: Is it your intention to provide this function or are you trying to use the one in java.lang.Math?
问题是:您是打算提供此功能还是尝试在 java.lang.Math 中使用该功能?
For the first, you have to make sure you're declaring that function.
首先,您必须确保声明了该函数。
For the second you have to make sure you're using the correct parameters types; see Math.
其次,您必须确保使用正确的参数类型;见数学。
Does your Math class have the abs method?
你的数学课有 abs 方法吗?
It seems like your Math class is shadowing the Math class that comes in the core of the language.
看起来您的 Math 类正在掩盖语言核心中的 Math 类。
Your Math class is loaded and the abs method could not be found (hence the "Cannot find symbol" message)
您的 Math 类已加载,但找不到 abs 方法(因此出现“找不到符号”消息)
回答by kristina
If you want your program to use Java's Math.abs (instead of an abs() method in the Math class you wrote), you can say:
如果您希望您的程序使用 Java 的 Math.abs(而不是您编写的 Math 类中的 abs() 方法),您可以说:
if(z - y == x && java.lang.Math.abs(y - z) == z)
...which isn't very pretty. In general, try not to name your classes the same as the ones in java.lang (or java.anything, really).
......这不是很漂亮。通常,尽量不要将类命名为与 java.lang(或 java.anything,真的)中的类相同。
回答by Todd Gamblin
In your compiler output, you have:
在您的编译器输出中,您有:
./Math.java:13: cannot find symbol
It looks like you're trying to write your own Mathclass, and it's shadowing java.lang.Math, which is builtin.
看起来您正在尝试编写自己的Math类,并且它java.lang.Math是内置的 shadowing 。
Do you have to have a class of your own called Math? If not, then just delete Math.javaand try to compile this again. If you doneed Math.java, then try renaming it to something else (like, say, MyMath.javawith public class MyMathdefined inside).
你必须有一个你自己的班级Math吗?如果没有,那么只需删除Math.java并尝试再次编译。如果确实需要Math.java,请尝试将其重命名为其他名称(例如,MyMath.javawith public class MyMathdefined inside)。

