使用方法计算圆周长

时间:2015-06-07 23:26:11

标签: java math methods

这可能相当简单,但我遇到的问题是返回“环”值,这是我的代码。

double circRad = 0;
double circum = 0;

if(userOption == 2){

        circRad = myRad();
        System.out.println("The Radius of the circle is " + circRad + " and it perimeter is "+ circum);

public static double myRad(){

    int i= 1;
    double circRad = 0;
    Scanner myInput=new Scanner(System.in);

    switch(i)
    {
        case 1:
        System.out.print("Please enter a positive Radius for the circle ");
        circRad = myInput.nextDouble();
        while(circRad <0){
            System.out.print("Please enter a POSITIVE Radius for the circle ");
            circRad = myInput.nextDouble();
            }
                ++i;
    }
    return circRad;
}

public static double myRad(double circRad){

    double circum = 0;

    circum = 2* Math.PI * circRad;

    return circum;
}

2 个答案:

答案 0 :(得分:0)

也许你错过了以下内容:

if(userOption == 2){
    circRad = myRad();
    circum  = myRad(circRad); // <-- missing setting local circum value
    System.out.println("The Radius of the circle is " + circRad + " and it perimeter is "+ circum);
}

答案 1 :(得分:0)

我只能用它呈现的方式来猜测代码是circRadcircum是全局变量,因此当你调用第一个myRad()时,只有circRad会发生变化。但是,circum仍为0。 如果你可以发送整个代码,它会让一切变得更容易。