private static void square() {
System.out.println("Would you like to find Area or Perimeter?");
String pOrA=sc.next();
if (pOrA.equals("Perimeter"));
{
System.out.println("What is the side length?");
Double sSide=sc.nextDouble();
double p = sSide *4;
System.out.print("The perimeter is " + p);
}
//this is where the error is happening
else if (pOrA.equals("Area"));
{
System.out.println("What is the side length?");
Double sSide2=sc.nextDouble();
double a = sSide2 * sSide2;
System.out.print("The Perimeter is " + a);
}
}
在 Eclipse 中,else-if 块出现错误,指出“令牌'else' 上的语法错误,删除此令牌”。老实说,我不知道为什么它不起作用。else-if 语句在自定义方法中的工作方式是否不同,因为它在 main 方法的早期工作正常。