我在 NetBeans 上不断收到一个错误,在读取Math.sqrt
和Math.pow
行时指出"错误,找不到符号",我完全不知道是什么导致了该确切的错误弹出。
我知道java中的Math
类已预装到编译器中。 最重要的是,即使我确实尝试导入 Math 文件夹,我也会收到关于import java.lang.Math;
的第二个错误的提示,它只是说它已经处于活动状态,不必导入。 我尝试使用"生成和清理"功能来尝试解决此问题,并关闭"保存时编译"功能,这两者都对 Netbeans 中的错误没有影响。
import java.util.Scanner;
import java.text.DecimalFormat;
public class Math {
public static void main(String[] args) {
double input, square, sqrt, cube;
DecimalFormat fmt = new DecimalFormat("0.###");
Scanner scan = new Scanner(System.in);
System.out.print("Welcome to the basic mathematics computation program. What number would you like to test today? ");
input = scan.nextFloat();
sqrt = Math.sqrt(input);
square = Math.pow(input, 2);
cube = Math.pow(input, 3);
System.out.println("The program is complete! n"
+ "The square of your number is " + fmt.format(square) + ", n"
+ "the cube of your number is " + fmt.format(cube) + ", n"
+ "and the square of your number is " + fmt.format(sqrt) + ".");
}
}
期望程序能够计算和打印任何正输入值的平方根、平方和立方,四舍五入到最接近的千分之一。 但是,错误消息阻止了任何输出。
这是因为你的类有相同的名称:"数学"。为了避免混淆,请使用全名:java.lang.Math.pow((或更改类的名称。