【问题标题】:Calculating circumference of circle using method使用方法计算圆的周长
【发布时间】:2015-08-22 08:23:42
【问题描述】:

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

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

【问题讨论】:

  • 那么到底是什么问题?
  • 您将circ初始化为0但没有将circRad的值来自myRad()。在输出半径和周长之前添加:“circum = myRad(circRad);”

标签: java math methods


【解决方案1】:

也许您缺少以下内容:

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

【讨论】:

    【解决方案2】:

    关于代码的呈现方式,我唯一能猜到的是circRadcircum 是全局变量,因此当您调用第一个myRad() 时,只有circRad 会发生变化。但是,circum 仍然为 0。 如果你能发送整个代码,一切都会变得更简单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多