这是代码:
public static void main(String args[])
{
int i=0;
int m=0;
double scale;
boolean exit;
Shape[] s = new Shape[10];
while(exit !=true)
{
System.out.print("\nChoose an option:\n"+
"1-Add a new circle\n"+
"2-Add a new rectangle\n"+
"3-Delete all shapes\n"+
"4-Scale all shapes\n"+
"5-Display perimeter of all shapes\n"+
"6-Display the area of all shapes\n"+
"7-Enter scale factor\n"+
"8-Exit program\n");
Scanner input = new Scanner(System.in);
m=input.nextInt();
if(i<=9)
{
switch (m)
{
case 1: Circle c = new Circle(0);
s[i]=c;
i++;
break;
case 2: Rectangle r = new Rectangle(1,1);
s[i]=r;
i++;
break;
case 3: s=null;
i=0;
break;
case 4: for(i=0; i<s.length; i++)
{
s[i].scaleShape();
}
break;
case 5: for(i=0; i<s.length; i++)
{
if(s[i] != null)
{
System.out.println(s[i].getPerimeter());
}
}
break;
case 6: for(i=0; i<s.length; i++)
{
if(s[i] != null)
{
System.out.println(s[i].getArea());
}
}
break;
case 7: do
{
System.out.println("\nEnter scale factor");
scale=input.nextDouble();
}
while(scale<0);
Shape.setScaleFactor(scale);
}
break;
case 8: System.out.println("Do you want to quit?");
break; //Nothing here since loop should terminate it.
//default: System.out.println("Number must be 1-8");
// break;
}
}
}
}
奇怪的是,编译器在案例 8 上给我一个错误说:
类型不匹配无法从 int 转换为 boolean。
但我没有将任何东西转换为布尔值
- 令牌“case”上的语法错误断言预期 - 令牌上的语法错误 :, ; 预期的
但是那里的所有命令都有分号
表达式必须返回一个值
为什么编译器的行为如此有趣?通常这样的错误很容易找到。到底是怎么回事?