简体   繁体   中英

Not getting correct output when using command line argument in java

//Square root of a no. using command line argument
class Calculator {
    double i;
    double x = Math.sqrt(i);
}

class SquareRoot {
    public static void main(String arg[]) {
        Calculator a = new Calculator();
        a.i = Integer.parseInt(arg[0]);
        System.out.println("The square root of " + a.i + " is " + a.x);
    }
}

My Output:

The square root of 64 is 0.0

What's wrong with my code?

try this :

class Calculator {
    double i, x;
    void squareRoot() {
        x = Math.sqrt(i);
    }
}

class SquareRoot {
    public static void main(String arg[]) {
        Calculator a = new Calculator();
        a.i = Integer.parseInt(arg[0]);
        a.squareRoot();
        System.out.println("The square root of " + a.i + " is " + a.x);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM