用于确定变量的IF STATEMENTS系列,JAVA

时间:2013-10-31 14:35:48

标签: java

目前这是我正在尝试的代码:

if(gear.isLeftHand())
    helix = (gear.getParentPair().beta());
else if (gear.isRightHand())
    helix = Math.PI - (gear.getParentPair().beta());
else if (gear.isSpur())
    helix = 0;
else 
    helix = 0;

double stepAng =  (thickness /radius) * helix;

然而它不起作用,这是因为'helix无法解析为变量'

我试图获取stepAng的值,具体取决于初始角度是左手还是右手,因此“螺旋”的值将根据此方向从不同的公式计算。

非常感谢任何帮助。

5 个答案:

答案 0 :(得分:5)

您需要实际声明helix,如果将其初始化为0,则可以取消两个表达式(我假设它是double,因为您引用了Math.PI 1}}):

double helix = 0;
if (gear.isLeftHand()) {
    helix = (gear.getParentPair().beta());
} else if (gear.isRightHand()) {
    helix = Math.PI - (gear.getParentPair().beta());
}

double stepAng =  (thickness / radius) * helix;

答案 1 :(得分:2)

您可能已在使用范围之外声明了 helix ,或者根本没有声明。

double helix = 0;

// The rest of the code follows

答案 2 :(得分:1)

您应该在helix声明之前声明if。当您尝试分配stepAng时,helix超出范围。

答案 3 :(得分:1)

如果您收到编译错误“无法解析变量XXX”(在您的情况下是螺旋),那么您需要在可在任何地方访问的范围中定义它,这里可能是方法的开始或您的类实例变量取决于根据你的需要。

第一种方式:

  public double getArea(){
    double helix=0.0;
    if(cond){ 
        helix=//some code
    }else{
        helix=//some code
    }
       // some code with helix
  }

第二种方式:

 public class AreaCalculator(){
   //highest scope based on requirement.
   private double helix;

   public double getArea(){
      double helix=0.0;
      if(cond){ 
         helix=//some code
      }else{
         helix=//some code
      }
      // some code with helix
   }//method
 }//class

答案 4 :(得分:0)

如果你想这样做:

else if (gear.isSpur())
    helix = 0;
else 
    helix = 0

你可以这样做:

double helix = 0;
if (gear.isLeftHand()) {
    helix = (gear.getParentPair().beta());
} else if (gear.isRightHand()) {
    helix = Math.PI - (gear.getParentPair().beta());
}


double stepAng =  (thickness / radius) * helix;