gpt4 book ai didi

java - 使用 for 循环而不是应用 Math.pow 方法计算功率

转载 作者:行者123 更新时间:2023-12-01 22:00:13 25 4
gpt4 key购买 nike

尝试一种使用 for 循环而不使用 Math.pow 来求数字幂的方法。对于这个结果,我只得到 2.0,因为它似乎没有通过循环返回。请帮忙。

public void test() {
{
double t = 1;
double b = 2; // base number
double exponent = 2;

for (int i = 1; i<=exponent; i++);
t = t*b;

System.out.println(t);

最佳答案

试试这个。

double t = 1; 
double b = 2; // base number
double exponent = 2;
for (int i = 1; i<=exponent; i++) t = t*b;
System.out.println(t);

关于java - 使用 for 循环而不是应用 Math.pow 方法计算功率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33642328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com