4 回答
TA贡献1810条经验 获得超5个赞
尝试这个
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");
}
也许你只想在循环之后打印出来
在 count 的值从未被用来评估循环之前。尝试有两个变量。
TA贡献1906条经验 获得超10个赞
好吧,我明白了,只需围绕 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贡献1804条经验 获得超3个赞
您可以使用下面的代码。
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贡献1802条经验 获得超4个赞
您可以翻译您的等式:
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");
}
添加回答
举报