【发布时间】:2020-06-09 08:13:39
【问题描述】:
您好,我是 java 初学者,我正在尝试编写代码来解决毕达哥拉斯定理。这是我到目前为止所做的,但是我不断收到错误,第 14 行的非法启动器以及第 31 行的“预期的类、接口或枚举”错误。
public class Pythagoras{
public static void main(String[] args){
double a1 = 5.2;
float a = (float)a1;
double b1 = 8.4;
float b = (float)b1;
double c1 = 0;
float c = (float)c1;
float resultC = (float)method (c);
System.out.println(resultC);
public static float method(float c){
if (c = 0){
float result = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
return result;
}
if (b = 0){
float result = Math.sqrt(Math.pow(c, 2) - Math.pow(a, 2));
return result;
}
if (a = 0){
float result = Math.sqrt(Math.pow(c, 2) - Math.pow(b, 2));
return result;
}
}
}
}
【问题讨论】:
-
你不能在方法中定义方法
-
您使用的是 IDE 吗?听起来你正在做编译时的猜测和检查。您的 IDE 应该会告诉您出了什么问题。
-
例如,
if (c = 0)不会编译。 -
你需要使用 if (a == 0)
-
@jhamon 您不能在方法中直接定义方法。如果你定义一个本地或匿名类,你可以。
标签: java algorithm if-statement math