1

您好,我是 Java 和 NetBeans 的新手,并且正在学习高级课程,我的课程为期 5 周,因此在 5 周内学习一门新的代码语言非常重要。无论如何,我有一个任务是创建一个名为 Movie 的类,该类包含电影名称和评级。提供获取和设置电影名称和评级的方法。创建一个名为 TestMovie 的类,该类创建三个具有不同名称和评级值的 Movie 实例,并打印有关每部电影的信息。我已经完成了代码,它通过了构建,但是我的教授想要一个程序工作和运行的屏幕截图,但我无法让 NetBeans 提出这个问题。我的书中删除了关于构建测试项目的章节。我可以在这里获得一些帮助或指示,这是我所做的代码:

package movie;

/**
 * 
 * @author Jason
 */
public class Movie {
    String movieRating;

    public Movie(String rated, String mtitle) {
        this.mrating = rated;
        this.title = mtitle;
    }

    public void setRating(String Rating) {
        movieRating = Rating;
    }

    // Get the rating
    public String getRating() {
        return movieRating;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public String toString() {
        return "Movie" + " title=" + getTitle() + " rating=" + getRating();
    }

    public static void main(String args[]) {
        Movie mv = new Movie("", "");
        mv.toString();
    }

    private String title;
    private String mrating;
}
4

3 回答 3

1

As an alternative to the other answers suggesting printing the output to the console, with the Netbeans' UI editor you can easily create a window with a label showing the result, which makes it slightly more fancy.

You can get details on how to this here. Here's an image from that page:

enter image description here

The full working code is here. As you can see, it's just a few extra lines.

于 2013-03-01T22:45:58.640 回答
1

您可以在控制台上运行测试,即创建一个MovieTest只有一个方法的类并创建三个 Movie ( OR )main实例/对象。在构造函数或方法中为它们分配值,然后使用方法或在.Movie m1, m2, m3;Movie[] movies;setprintprintlnSystem.out

类似于以下内容:

public class MovieTest {
    public static void main(String[] args) {
        Movie[] movies = new Movie[] {new Movie("R1", "T1"), new Movie("R2", "T2"), new Movie("R3", "T3)";

        for (Movie i : movies) {
            System.out.println(i.toString());
        }    
    }
}

然后以截屏结果结束。

于 2013-03-01T22:28:11.413 回答
1

您的应用程序不打印任何输出,因为您调用了 toString(),但您不打印它的结果。

一个使用数据创建 3 个 Movie 实例的示例,将它们打印出来,然后制作控制台应用程序的屏幕截图。

public static void main(String args[]) {
    List<Movie> movieList = new ArrayList<Movie>(3);

    Movie mv1 = new Movie("very Good", "Testfilm 1");
    movieList add(mv1);
    mv1 = new Movie("good", "Testfilm 2");
    movieList add(mv1);
    mv1 = new Movie("not good", "Testfilm 2");
    movieList add(mv1);

    for (Movie m : movieList) {
         System.out.println(m.toString());
    }

}
于 2013-03-01T22:30:50.663 回答