我们的教授给了我们一个可以处理浮点数的代码,但是它没有编译。
package numeric;
import java.math.*;
public class Floating {
public static double fl(double x) {
BigDecimal decX = new BigDecimal(Double.toString(x));
int r = decX.scale();
decX = decX.movePointRight(r);
String intX = decX.toBigInteger().abs().toString();
int l = intX.length();
decX = decX.movePointLeft(l);
decX = decX.setScale(digits, BigDecimal.ROUND_HALF_UP);
decX = decX.movePointLeft(r - l);
return decX.doubleValue();
}
public static void setDigits(int nDigits) {
digits = nDigits;
}
public static int getDigits(int nDigits) {
return digits;
}
static int digits = 16;
public static double Absolut(double x, double y) {
return Math.abs(x-y);
}
}
但是当我尝试代码
import numeric.Floating;
public class Test {
public static void main(String[] args) {
double x = 123.0456785;
System.out.println("The number " + x);
for (int m = 17; m > 0; m--) {
System.out.println("with " + m + " significant digits");
Floating.setDigits(m);
System.out.println(Floating.fl(x));
}
Floating.setDigits(3);
double x1 = Floating.fl(100), x2 = Floating.fl(.1);
System.out.println(x1 + " + " + x2 + " = " + Floating.fl(x1 + x2));
System.out.println(133 + " + " + 0.921 + " = " + Floating.fl(133+0.921));
Floating.setDigits(5);
}
}
它表示软件包数字不存在,并且无法访问Floating。代码有问题吗?
我还有其他需要处理浮点数的代码,但是由于这个问题,它们都不起作用。
答案 0 :(得分:2)
默认包中的类无法导入包中的类。将Test
移至numeric
package numeric;
public class Test {
...
答案 1 :(得分:0)
只需进行一些小修改:
然后运行: