我需要构建一个执行此操作的循环:3 = Math.log10(数字的平方结果)/Math.log10(输入的数字)number_to_be_cubed
double cubed;
double answer;
answer = 1;
cubed = 0;
while (cubed <= 3) {
cubed = (double) Math.log( answer )/Math.log( number_to_be_cubed );
answer ++;
}
double answer_for_cubed = answer;
System.out.println("answer_for_cubed " + answer_for_cubed);
基于A ^ 3 = B和3 = logB / logA是同一件事的事实。相反,我知道我可以使用math.pow,但是我正在尝试用日志解决x ^ 3。我认为由于Java处理数字的方式而无法正常工作。这可能吗?我是否以错误的方式循环?我一直在互联网上到处寻找,却没有发现这样的问题。
答案 0 :(得分:0)
这有效
public static void main(String[] args) {
Double cubed = 0d;
Double answer = 0d;
while (cubed.compareTo(3d) <0) {
answer ++;
cubed = Math.log( answer )/Math.log( 4 );
System.out.println(cubed + " " + answer);
}
double answer_for_cubed = answer;
System.out.println("answer_for_cubed " + answer_for_cubed);
}
结果是:
answer_for_cubed 64.0