所以我創建了一個方法,我想計算任何給定整數的平方根。對於像27這樣的x,我希望它顯示3√3。或28,2√7等等。我會提前告訴你,我知道這種方法還有其他問題 - 我打算解決它們。運行自定義平方根函數(Java)時出錯
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);
}
}
執行任何不會產生乾淨根的整數後,會收到一個錯誤消息「/ zero」。在上面,我有一些線標記錯誤據說發生的地方。提前致謝!
你在期待「i^2」做什麼 – afzalex