[英]java using NetBeans IDE 8.0.2
I am having trouble clearing the following error '(' or '[' expected on the second line of case 2 and case 3. The code I have written is newAnimal.displayInfo(); 我无法清除情况2和情况3第二行中的以下错误'('或'['。我编写的代码是newAnimal.displayInfo();。
I am not sure why I get this error on case 2 and 3 but not case 1. Not sure what I am doing wrong. 我不确定为什么在情况2和情况3中会出现此错误,但在情况1中却没有。我不确定我在做什么错。 Any assistance/guidance will be appreciated.
任何帮助/指导将不胜感激。
Here is what the code looks like: 代码如下所示:
package animalinfo;
import java.util.Scanner;
public class AnimalInfo
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
Scanner input = new Scanner (System.in);
Animal newAnimal;
int quit = 4;
while(-4 != quit);
{
System.out.println("\n1) Camel" +
"\n2)Penguin" +
"\n3) Tortoise" +
"\n4) Exit Program.");
System.out.print("Please select an amimalfrom the list.");
int choice = input.nextInt();
switch (choice)
{
case 1:
newAnimal = new Camel();
newAnimal.displayInfo();
break;
case 2:
newAnimal = new Penguin
newAnimal.displayInfo();
break;
case 3:
newAnimal = new Tortoise
newAnimal.displayInfo();
break;
case 4:
System.out.println ("Thank you for making your selections.");
break;
}
}
}
}
while(-4 != quit);
Get rid of the semicolon, should just be 摆脱分号,应该只是
while (-4 != quit)
{
/*Code here*/
}
and yes, when you have new Penguin
and new Tortoise
, you are missing the parentheses and semicolon 是的,当您有
new Penguin
和new Tortoise
,会丢失括号和分号
It seems like you're missing parentheses after creating the new objects. 创建新对象后,似乎缺少括号。 So this:
所以这:
newAnimal = new Penguin
should become this: 应该变成这个:
newAnimal = new Penguin();
This is because you're setting newAnimal to a new instance of a Penguin object, and to create that new instance you must call the constructor of the Penguin class to create the object. 这是因为要将newAnimal设置为Penguin对象的新实例,并且要创建该新实例,必须调用Penguin类的构造函数来创建该对象。
Also, as Jurko stated, your while loop is set up incorrectly. 另外,正如Jurko所说,您的while循环设置不正确。
while(-4 != quit);
You must remove the semicolon, otherwise the loop will indefinitely run without executing the code you have underneath it. 您必须删除分号,否则循环将无限期地运行而不执行您在其下的代码。 The correct syntax for a while loop is
while循环的正确语法是
while (-4 != quit) {
// Code to repeat here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.