Square root in Java

Should I write in Java like that? If not, how should I write it?

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);
    }
}

Solution

Like other languages, M / N will be an integer, and for M = 1, n = 2, you will get m / N = 0

If you want a non integer result, consider m and N as double precision - or convert them to it in the evaluation

Example:

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

The output will be:

1.0
3.0
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>