首先是java狗/ myDogs练习

时间:2014-10-19 23:05:11

标签: java

我在编译代码时遇到了麻烦,即:

     Class Dog {
 String name = null;
 public static void main (String[] args) {
   //make a Dog object and access it

   Dog dog1 = new Dog();
   dog1.bark();
   dog1.name = "Bart";

   //now make a Dog array
   Dog[] myDogs = new Dog[3];
   //and put some dogs in it
   myDogs[0] = new Dog();
   myDogs[1] = new Dog();
   myDogs[2] = dog1;

   //now access the Dogs using the array references
   myDogs[0].name = "Fred";
   myDogs[1].name = "George";

   //Hmmm.. what is myDogs[2] name?
   System.out.print("last name's dog is  - ");
   System.out.println(myDogs[2].name);

   // now loop through the array
   // and tell all dogs to bark
   int x = 0;
   while (x < myDogs.length) {
      myDogs[x].bark();
      x = x + 1;
      }
    }
   public void bark() {
    System.out.println(name + "Ruff!!");
   }
   public void eat() { }
   public void ChaseCat() { }
}   

并且javac给了我这个:

C:\Users\User>javac Dog.java
Dog.java:1: error: class, interface, or enum expected
Class Dog {
^
Dog.java:3: error: class, interface, or enum expected
 public static void main (String[] args) {
             ^
Dog.java:7: error: class, interface, or enum expected
       dog1.bark();
       ^
Dog.java:8: error: class, interface, or enum expected
       dog1.name = "Bart";
       ^
Dog.java:11: error: class, interface, or enum expected
       Dog[] myDogs = new Dog[3];
       ^
Dog.java:13: error: class, interface, or enum expected
       myDogs[0] = new Dog();
       ^
Dog.java:14: error: class, interface, or enum expected
       myDogs[1] = new Dog();
       ^
Dog.java:15: error: class, interface, or enum expected
       myDogs[2] = dog1;
       ^
Dog.java:18: error: class, interface, or enum expected
       myDogs[0].name = "Fred";
       ^
Dog.java:19: error: class, interface, or enum expected
       myDogs[1].name = "George";
       ^
Dog.java:22: error: class, interface, or enum expected
       System.out.print("last name's dog is  - ");
       ^
Dog.java:23: error: class, interface, or enum expected
       System.out.println(myDogs[2].name);
       ^
Dog.java:27: error: class, interface, or enum expected
       int x = 0;
       ^
Dog.java:28: error: class, interface, or enum expected
       while (x < myDogs.length) {
       ^
Dog.java:30: error: class, interface, or enum expected
              x = x + 1;
              ^
Dog.java:31: error: class, interface, or enum expected
              }
              ^
Dog.java:33: error: class, interface, or enum expected
    public void bark() {
           ^
Dog.java:35: error: class, interface, or enum expected
    }
    ^
Dog.java:36: error: class, interface, or enum expected
    public void eat() { }
           ^
Dog.java:37: error: class, interface, or enum expected
    public void ChaseCat() { }
           ^
20 errors

我确实尝试编译其他Java“项目”,它们运行没有任何问题。 我的java有什么问题,或者我的代码是问题的根源?感谢。

2 个答案:

答案 0 :(得分:1)

Class应为小写。 class是用于定义类的Java关键字。它也应该是public,但这是另一回事。

答案 1 :(得分:0)

它&#34; class&#34;,而不是&#34; Class&#34;。替换它,它应该工作!