4 回答

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
嘗試這個(gè)
int count = 0;
double i = 1;
while(i >= 0.0001){
i = i/2;
count++;
System.out.println("You have to divide 1 " + count + " times to get 0.0001");
}
也許你只想在循環(huán)之后打印出來
在 count 的值從未被用來評估循環(huán)之前。嘗試有兩個(gè)變量。

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊
好吧,我明白了,只需圍繞 public class Main { 切換一些東西
public static void main(String[] args) {
int count = 0;
for (double i = 1; i >= 0.0001; count++ ) {
i = i / 2;
}
System.out.println("You have to divide 1 " + count + " times to get 0.0001");

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊
您可以使用下面的代碼。
int count = 0;
for(double i = 1; i>= 0.0001; ){
i = i/2;
count++;
System.out.println("You have to divide 1 " + count + " times to get " + i);
}

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以翻譯您的等式:
x / y^n <= z
到
n >= log(x/z) / log(y)
因此它很簡單:
public static void main(String[] args) {
System.out.println("You have to divide 1 " + Math.ceil(Math.log(1.0 / 0.0001) / Math.log(2)) + " times by 2 to get 0.0001");
}
添加回答
舉報(bào)