【发布时间】:2016-08-08 05:28:00
【问题描述】:
所以我创建了一个方法来计算任何给定整数的平方根。对于像 27 这样的 x,我希望它显示 3√3。或者对于 28、2√7 等等。我会提前告诉你,我知道这种方法还有其他问题——我打算解决这些问题。
public static void root(int x) {
int [] a = new int [x]; //list of outside integers for each i
int b = x; //inside
int c = 1; //product of a[]
double sqrt = Math.sqrt(x);
if (sqrt != (int) sqrt) {
for (int i=2; i < x; i++) {
a[i] = 1;
while (x%(i^2) ==0) { //ERROR HERE
a[i] = a[i] * i;
b = b/(a[i])^2;
}
c = c * a[i];
}
System.out.println(b+"\u221A" + c);
}
else {
System.out.println((int) sqrt);
}
}
在执行任何不产生干净根的整数后,我收到一条错误消息“/零”。上面我标记了据说发生错误的地方。提前致谢!
【问题讨论】:
-
您希望
i^2做什么
标签: java methods runtime-error square-root