在用Java封装时接受用户输入

时间:2014-10-26 00:51:45

标签: java input encapsulation

到目前为止,我有两个类,Movie类和MovieTestDrive类。我要获得电影的标题,流派和评级,然后将它们放入数组并调用playIt()方法。 当我尝试运行代码时,它将无法工作,我不知道这是否是因为我在扫描程序运行不正确的同时封装了我的数据。任何帮助将不胜感激。

到目前为止我所拥有的:

电影课

    // Create instance variables for the Movie class.
    private String title;
    private String genre;
    private int rating;

    public String getTitle() {
        return title;
    }

    public String getGenre() {
        return genre;
    }

    public int getRating() {
        return rating;
    }

    public void setTitle(String newTitle) {
        title = newTitle;
    }

    public void setGenre(String newGenre) {
        genre = newGenre;
    }

    public void setRating(int newRating) {
        rating = newRating;
    }

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

MovieTestDrive类

public static void main(String[] args) {

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

    // Create an instance of the Movie class.
    Movie one = new Movie();

    // Take in user input and assign input to the variables for Movie 1.
    System.out.println("Please enter the title of Movie 1");
    one.setTitle = input.next();

    System.out.println("Please enter the genre of Movie 1");
    one.setGenre = input.next();

    System.out.println("Please enter the rating of Movie 1");
    one.setRating = input.nextInt();

    // Create the second instance of the Movie class.
    Movie two = new Movie();

    // Take in user input and assign input to the variables for Movie 2.
    System.out.println("Please enter the title of Movie 2");
    two.setTitle = input.next();

    System.out.println("Please enter the genre of Movie 2");
    two.setGenre = input.next();

    System.out.println("Please enter the rating of Movie 2");
    two.setRating = input.nextInt();

    // Create the last instance of the Movie class.
    Movie three = new Movie();

    // Take in user input and assign input to the variables for Movie 2.
    System.out.println("Please enter the title of Movie 3");
    three.setTitle = input.next();

    System.out.println("Please enter the genre of Movie 3");
    three.setGenre = input.next();

    System.out.println("Please enter the rating of Movie 3");
    three.setRating = input.nextInt();

    // Print the information.
    System.out.println("Movie 1 Title: " + one.getTitle());
    System.out.println("Move 1 Genre: " + one.getGenre());
    System.out.println("Movie 1 Rating: " + one.getRating());
    System.out.println("");
    // Call the playIt() method for each movie.
    one.playIt();

    System.out.println("Movie 2 Title: " + two.getTitle());
    System.out.println("Move 2 Genre: " + two.getGenre());
    System.out.println("Movie 2 Rating: " + two.getRating());
    System.out.println("");
    two.playIt();

    System.out.println("Movie 3 Title: " + three.getTitle());
    System.out.println("Move 3 Genre: " + three.getGenre());
    System.out.println("Movie 3 Rating: " + three.getRating());
    System.out.println("");
    three.playIt();
}

1 个答案:

答案 0 :(得分:0)

应该是Scanner input = new Scanner(System.in);

当你设置电影标题时,它应该是one.setRating(input.nextInt());而不是one.setRating = input.nextInt();

如果输入one.setRating,您将看到方法setRating(int NewRating)将int作为参数,这意味着在调用此方法时必须传入int(input.nextInt())。