以下是代码:
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转换为布尔值。
但我没有把任何东西转换成布尔
令牌"case“上的-syntax错误断言令牌上的预期-syntax错误:
但那里所有的命令都有分号
表达式必须返回值。
为什么编译器表现得这么有趣?通常,这样的错误很容易发现。怎么一回事?
发布于 2012-10-11 03:14:01
while(scale<0);
Shape.setScaleFactor(scale);
} // Remove this parenthesis.
break;发布于 2012-10-11 03:08:35
你的问题出在7的情况下
case 7: do
{
System.out.println("\nEnter scale factor");
scale=input.nextDouble();
}
while(scale<0);
Shape.setScaleFactor(scale);
}注意额外的关闭大括号:这将关闭您的switch语句,使您的case 8成为孤儿。
发布于 2012-10-11 03:07:54
} // <-- Why is this here?
break;
case 8: System.out.println("Do you want to quit?");您将用一个额外的}结束开关语句。把它移开,一切都会成功的。
https://stackoverflow.com/questions/12831571
复制相似问题