“___”无法解析为变量

"___" cannot be resolved to a variable

提问人:zebra14420 提问时间:10/8/2014 最后编辑:zebra14420 更新时间:10/8/2014 访问量:537

问:

关于如何使这些变量在定义它们的 if 语句之外可用的任何提示?

    Scanner kb = new Scanner(System.in);
    System.out.println("Please enter a length of a side of a triangle: ");
    double num1 = kb.nextDouble();
    System.out.println("Please enter a length of a side of a triangle: ");
    double num2 = kb.nextDouble();
    System.out.println("Please enter a length of a side of a triangle: ");
    double num3 = kb.nextDouble();

    if (num1>num2){
        if (num1>num3){
            double hypo = num1;
            double side1 = num2;
            double side2 = num3;
        }else{
            double hypo = num3;
            double side1 = num1;
            double side2 = num2; }
    }else if (num2>num3){
        double hypo = num2;
        double side1 = num1;
        double side2 = num3; }

    if ((hypo*hypo)-((side1*side1)+(side2*side2)) == 0){

    }

我也尝试过这样做:

    Scanner kb = new Scanner(System.in);
    System.out.println("Please enter a length of a side of a triangle: ");
    double num1 = kb.nextDouble();
    System.out.println("Please enter a length of a side of a triangle: ");
    double num2 = kb.nextDouble();
    System.out.println("Please enter a length of a side of a triangle: ");
    double num3 = kb.nextDouble();
    final double hypo;
    final double side1;
    final double side2;

    if (num1>num2){
        if (num1>num3){
            hypo = num1;
            side1 = num2;
            side2 = num3;
        }else{
            hypo = num3;
            side1 = num1;
            side2 = num2; }
    }else if (num2>num3){
        hypo = num2;
        side1 = num1;
        side2 = num3; }

    if ((hypo*hypo)-((side1*side1)+(side2*side2)) == 0){

    }

我昨天在网站上得到了这方面的帮助,但不明白该解决方案是如何获得的。一个解释会很好

Java 变量

评论

1赞 scrappedcola 10/8/2014
您可以在线查看一些关于该语言的教程。这个描述变量范围

答:

2赞 Elliott Frisch 10/8/2014 #1

是的。将声明向上移动,以便变量仍然具有作用域,

double hypo = num3;
double side1 = num2;
double side2 = num1; 
if (num1 > num2){
  if (num1 > num3){
    hypo = num1;
    side1 = num2;
    side2 = num3;
  } else {
    hypo = num3;
    side1 = num1;
    side2 = num2;
  }
} else if (num2 > num3) {
  hypo = num2;
  side1 = num1;
  side2 = num3; 
}