输入和显示数据

时间:2014-10-23 20:00:05

标签: java arrays eclipse for-loop

我需要输入3部电影的三个细节然后让它们显示,一些问题不会让我输入数据,显示的数据只包含一个单词。

这是作业问题:

为每个私有数据成员编写适当的getter和setter方法 你还应该编写一个合适的构造函数你的应用程序应该包括一个测试人员/驱动 class,它创建Movie类的三个实例并将它们存储在一个数组中。你的测试员 class应该使用for循环来设置Movie对象的标题,流派和等级。你应该 也可以使用for循环,你应该遍历数组并显示每个的标题,流派和评级 数组的元素,并为每个对象调用playIt()方法。

有人可以告诉我代码有什么问题吗?如果不是太多问你可以告诉我如何使用数组和for循环。我正在使用eclipse。

驱动程序类

class Movie {

    //private instances
    private String title = "unassigned";
    private String genre = "unassigned";
    private int rating = 0;

    void playIt() {
        System.out.println(" - Now Playing");
    }

    //getters
    public String getTitle(){
        return title;
    }

    public String getGenre(){
        return genre;
    }

    public int getRating(){
        return rating;
    }

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

    public void setGenre(String genre){
        this.genre = genre;
    }

    public void setRating(int rating){
        this.rating = rating;
    }

}//end class

测试员

import java.util.Scanner;

public class Movie_Tester {

    public static void main(String [] args){

        Movie one = new Movie();
        Movie two = new Movie();
        Movie three = new Movie();

        //declare variables
        String title1, title2, title3;
        String genre1, genre2, genre3;
        int rating1, rating2, rating3;

        Scanner input = new Scanner(System.in);
        Scanner user_input = new Scanner(System.in);
        Scanner userInput = new Scanner(System.in);

        //Enter Details For Movie 1
        System.out.println("Please Enter The Title Of Movie 1: ");
        title1 = input.next();
        System.out.println("Please Enter The Genre Of Movie 1: ");
        genre1 = user_input.next();
        System.out.println("Please Enter The Rating Of Movie 1: ");
        rating1 = userInput.nextInt();
        System.out.println(" ");

        //Enter Details For Movie 2
        System.out.println("Please Enter The Title Of Movie 2: ");
        title2 = input.next();
        System.out.println("Please Enter The Genre Of Movie 2: ");
        genre2 = user_input.next();
        System.out.println("Please Enter The Rating Of Movie 2: ");
        rating2 = userInput.nextInt();
        System.out.println(" ");

        //Enter Details For Movie 3
        System.out.println("Please Enter The Title Of Movie 3: ");
        title3 = input.next();
        System.out.println("Please Enter The Genre Of Movie 3: ");
        genre3 = user_input.next();
        System.out.println("Please Enter The Rating Of Movie 3: ");
        rating3 = userInput.nextInt();
        System.out.println(" ");

        //Set data for Movie one
        one.setTitle(title1);
        one.setGenre(genre1);
        one.setRating(rating1); 
        one.playIt();

        //Print data to screen
        System.out.println("Movie 1 Title = : " + one.getTitle());
        System.out.println("Movie 1 Genre = : " + one.getGenre());
        System.out.println("Movie 1 Rating = : " + one.getRating());        
        System.out.println(" ");

        //Set data for Movie two
        two.setTitle(title2);
        two.setGenre(genre2);
        two.setRating(rating2);
        two.playIt();

        //Print data to screen
        System.out.println("Movie 2 Title = : " + two.getTitle());
        System.out.println("Movie 2 Genre = : " + two.getGenre());  
        System.out.println("Movie 2 Rating = : " + two.getRating());        
        System.out.println(" ");

        //Set data for Movie three
        three.setTitle(title3);
        three.setGenre(genre3);
        three.setRating(rating3);
        three.playIt();

        //Print data to screen
        System.out.println("Movie 3 Title = : " + three.getTitle());
        System.out.println("Movie 3 Genre = : " + three.getGenre());    
        System.out.println("Movie 3 Rating = : " + three.getRating());  
        input.close();
        user_input.close();
        userInput.close();
        System.out.println(" ");


    }//end main method
}//end class

当我运行它时,这就是我得到的:

Please Enter The Title Of Movie 1: 
Step Brothers

Please Enter The Genre Of Movie 1: 
Comedy

Please Enter The Rating Of Movie 1: 
4

Please Enter The Title Of Movie 2: 

Please Enter The Genre Of Movie 2: 
Crime Comedy

Please Enter The Rating Of Movie 2: 
4

Please Enter The Title Of Movie 3: 
O Brother, Where Art Thou?

Please Enter The Genre Of Movie 3: 

Please Enter The Rating Of Movie 3: 
5

 - Now Playing

Movie 1 Title = : Step

Movie 1 Genre = : Comedy

Movie 1 Rating = : 4

 - Now Playing

Movie 2 Title = : Brothers

Movie 2 Genre = : Crime

Movie 2 Rating = : 4

 - Now Playing

Movie 3 Title = : O

Movie 3 Genre = : Comedy

Movie 3 Rating = : 5

1 个答案:

答案 0 :(得分:0)

您遇到的问题是next() Scanner方法仅获取下一个令牌,而不是整行。您需要nextLine()方法。

我不知道你是否已经在课堂上学到了这一点,但通常你想要Scanner使用try-with-resources块。这将确保无论如何,您的Scanner对象都已关闭(它是在finally块中关闭它们的语法糖。)

对于每个输入,您也不需要单独的扫描程序,但因为您将需要readLine()nextInt(),所以您需要其中两个(Scanner如果您在使用readLine()后尝试nextInt(),则会出现问题。

通过不使用循环,你也在艰难地做事。当您使用for循环时,代码更简单,更短。您的老师要求您使用数组和循环,而不向您解释如何使用它们?是不是有教科书或一些你可以参考的课堂笔记?

import java.util.Scanner;

public class MovieTester {

    public static void main(String [] args){

        Movie[] movies = new Movie[3];

        try (Scanner string_input = new Scanner(System.in);
            Scanner int_input = new Scanner(System.in);) // Try with resources
        {
            for (int i = 0; i < 3; i++) {
                Movie movie = new Movie();

                // Enter Details For Movie
                System.out.println("Please Enter The Title Of Movie: ");
                movie.setTitle(string_input.nextLine());
                System.out.println("Please Enter The Genre Of Movie: ");
                movie.setGenre(string_input.nextLine());
                System.out.println("Please Enter The Rating Of Movie: ");
                movie.setRating(int_input.nextInt());
                System.out.println();

                movies[i] = movie;
            }

            // Play Movies
            for (int i = 0; i < movies.length; i++) {
                Movie movie = movies[i];

                System.out.println("Title: " + movie.getTitle());
                System.out.println("Genre: " + movie.getGenre());
                System.out.println("Rating: " + movie.getRating());
                movies[i].playIt();
                System.out.println();
            }
        }

    }//end main method
}//end class