2011-11-13 32 views
1

我在玩java,遇到以下问题。打印声明给出“无法找到符号”消息

我有以下类

class Training{ 

    public static void main(String[]args){ 

     book firstBook = new book("Hamlet","William Shakespeare"); 
     book secondBook = new book("Heart of Darkness", "Joseph Conrad"); 
     book thirdBook = new book("Database Design","M Hernandez"); 

     System.out.println(); 
     System.out.println("Total number of books is " + book.noOfBooks + "\n"); 

     System.out.println(); 
    } 
} 

public class book { 

    private String name; 
    private String author; 
    private int id; 
    public static int noOfBooks = 0; 

    public book(String n, String a){ 
     name = n; 
     author = a; 
     id = ++noOfBooks; 

     System.out.printf("The book you've just created is %s\n", this); 
    } 

    public String toString(){ 
     return String.format("%s by %s id %d", name, author, id); 
    } 

    public String getName(){ 
     return name; 
    } 
    public String getAuthor(){ 
     return author; 
    } 
    public int getID(){ 
     return id; 
    } 
} 
public class whatDay { 

    System.out.println(); 
} 

的NetBeans抛出一个消息在whatDay类print语句“无法找到符号”。

任何想法可能是什么问题?

回答

1

问题是System.out.println();因为你没有把它放到方法中。

尝试将System.out.println()放入方法中。 例如:

public class WhatDay { 
    // Constructor 
    public WhatDay() { 
     System.out.println() 
    } 
} 

顺便说一句:你应该开始用大写字母类名。

玩得开心用Java :)

+1

嗨mhp,非常感谢。我工作:) – zan

+0

不客气。 – mhmpl

+0

@赞,如果这个答案适合你,请[标记为已接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)。 –

1

错误即将到来,因为您在whatDay类中的println调用未包含在方法中。