// Create a class called Dog containing two String: name and says. In main(), create two dogs objects
and assign it to spot's object.
class Dog {
String name;
String says;
}
public class DogSays {
public static void main(String[] args) {
Dog D1 = new Dog();
Dog D2 = new Dog();
D1.says = "Woof!";
D1.name = "Scruffy!";
D2.says = "Bark!";
D2.name = "Spot!";
System.out.println("Hi! My name is " + D1.name);
System.out.println(D1.says);
System.out.println("Rooooooooowr! I'm " + D2.name);
System.out.println(D2.says);
}
}
我已经检查了一个小时,我不知道出了什么问题。我得到了一个类,枚举或接口错误,也有一个未闭合的字符文字。我认为它与引号有关。
答案 0 :(得分:2)
问题“Unclosed character literal”在这里:
//Exercise2.5 Create a class called Dog containing two String: name and says. In main(), create two dogs objects
and assign it to spot's object.
成功:
//Exercise2.5 Create a class called Dog containing two String: name and says. In main(), create two dogs objects
//and assign it to spot's object.
如果没有前导//
或未/*
和*/
对中,您就不会有杂散评论......
答案 1 :(得分:1)
唯一想到的是:
//Exercise2.5 Create a class called Dog containing two String: name and says. In main(), create two dogs objects
and assign it to spot's object.
如果这正是您在课堂上编写的,那么,您的评论的第二行会出现错误:and assign it to spot's object.
。在Java中,//
表示一行注释,因此您必须执行以下操作:
//Exercise2.5 Create a class called Dog containing two String: name and says. In main(), create two dogs objects
//and assign it to spot's object.
或者
/*Exercise2.5 Create a class called Dog containing two String: name and says. In main(), create two dogs objects
and assign it to spot's object. */
与//
相反,//
指示编译器跟随/* */
并且在同一行上的任何其他文本由于它们是注释而被忽略,{{1}}个字符表示注释可以跨越任意行的块。