有关如何使这些变量在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){
}
我昨天在网站上得到了这方面的帮助,但不明白该解决方案是如何获得的。解释会很棒
答案 0 :(得分:2)
是。移动声明以使变量仍具有范围
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;
}