gpt4 book ai didi

java - Java 中的平方根

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:27:38 27 4
gpt4 key购买 nike

我应该像那样用 Java 编写代码吗?如果不是,应该怎么写?

enter image description here

import java.util.*;
public class Soru {
public static void main(String[] args) {
int m,n,c;
double f=0;
Scanner type = new Scanner(System.in);
System.out.print("Enter the m value :");
m=type.nextInt();
System.out.print("Enter the n value :");
n=type.nextInt();
System.out.print("Enter the c value :");
c=type.nextInt();
f=Math.pow(c, m/n);
System.out.println("Resul:"+f);
}
}

最佳答案

与其他语言一样,m/n 将是一个整数,对于 m=1,n=2,您将得到 m/n= 0

考虑将 mn 作为 double - 或者在评估中将它们转换为它,如果你想要一个非整数结果。

例子:

int m = 1, n = 2, c = 9;
System.out.println(Math.pow(c, m/n));
System.out.println(Math.pow(c, ((double)m)/n));

将产生:

1.0
3.0

关于java - Java 中的平方根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19786830/

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