用Java切换语句,指针/引用和对象数组

时间:2016-12-11 05:51:19

标签: java arrays object reference switch-statement

我目前是计算机科学的初学者,我一直在制作电影图书馆"申请上课。在我的原始代码中,似乎无论何时我调用.printLibrary()和.averageRating()方法都没有打印出来,即使我已经初始化了数组对象并在其中输入新信息。

public class MovieApp {

   public static void main(String [] args) {

      MovieUX mu = new MovieUX();
      mu.run();

 }

}

import java.util.Scanner;

public class MovieUX {

public void run(){

    Scanner input = new Scanner (System.in);
    char continueProcess = 'y';
    while(continueProcess == 'y'){

    System.out.println(" Welcome to the Movie Data Base");
    System.out.println("-----------------------------------------------");
    System.out.println("Please select from the following options: (Please make sure to create your library"+
    " and movie(s) first.)");
    System.out.println("1.  Create a library.");
    System.out.println("2.  Create a Movie.");
    System.out.println("3.  Add a movie to a library.");
    System.out.println("4.  Add an actor to a movie.");
    System.out.println("5.  Print a library.");
    System.out.println("6.  Print average movie rating for a library.");
    System.out.println("-----------------------------------------------");

    int option = input.nextInt();
    int i = 0;
    int j = 0;
    Library [] libraryArr = new Library[10];
    for(int a=0; a<libraryArr.length; a++)
        libraryArr[a] = new Library(0);
    Movie [] movieArr = new Movie[10];
    for(int b=0; b<movieArr.length; b++)
        movieArr[b] = new Movie("","",0,0.0,0);

    switch (option){
        case 1:
            System.out.println("Please input the amount of movies in your library");
            int numOfMovies = input.nextInt();
            libraryArr [i] = new Library(numOfMovies);
            i++;
            break;
        case 2:
            System.out.println("Please enter the name of the movie");
            String title = input.next();
            System.out.println("Please enter the director of the movie");
            String director = input.next();
            System.out.println("Please enter the year the movie was released");
            int year = input.nextInt();
            System.out.println("Please enter the movie's rating");
            double rating = input.nextDouble();
            System.out.println("Please enter the number of actors");
            int maxActors = input.nextInt();

            movieArr [j] = new Movie(title, director, year, rating, maxActors);
            j++;
            break;
        case 3:
            System.out.println("Below is your list of movies, select the corresponding movie" +
            " to add it to your desired library.");
            System.out.println();

            for(int k=0; k<movieArr.length; k++){
                System.out.println((k)+". "+movieArr[k].getTitle()); 
            }
            int movieChoice = input.nextInt();

            System.out.println("Below are some libraries, chose the library that you wish to" +
            " add your movie into.");
            System.out.println();

            for(int l=0; l<libraryArr.length; l++){
                System.out.println("Library #" +(l));
            }
            int desiredLibrary = input.nextInt();

            libraryArr[desiredLibrary].addMovie(movieArr[movieChoice]);
            break;
        case 4:
            System.out.println("Below is your list of movies, please select the movie"+
            " that you desire to add an actor into.");
            System.out.println();

            for(int k=0; k<movieArr.length; k++){
                System.out.println((k)+". "+movieArr[k].getTitle()); 
            }
            movieChoice = input.nextInt();

            System.out.println("Please input the actor's name that you would like to"+
            " add to your movie.");
            String addedActor = input.next();

            movieArr[movieChoice].addActor(addedActor);
            break;
        case 5:
            System.out.println("To print out a library's contents, please select from the following"+
            " list.");

            for(int l=0; l<libraryArr.length; l++){
                System.out.println("Library #" +(l));
            }
            desiredLibrary = input.nextInt();

            libraryArr[desiredLibrary].printLibrary();
            break;
        case 6:
            System.out.println("To print out the average movie rating for a library, please"+
            " select a library from the following list.");

            for(int l=0; l<libraryArr.length; l++){
                System.out.println("Library #" +(l));
            }
            desiredLibrary = input.nextInt();

            System.out.println(libraryArr[desiredLibrary].averageRating());
            break;
        default:
            System.out.println("Not a valid input.");
    }
    System.out.println("If you would like to continue customizing and adjusting your library"+
    " and movie settings, please input 'y'. Otherwise, press any key and hit 'enter'.");
    continueProcess = input.next().charAt(0);
}

}

}

 import java.util.Arrays;

  public class Movie {

   private String title;
   private String director;
   private int year;
   private double rating;
   private String [] actors;
   private int numberOfActors;

   public Movie(String title, String director, int year, double rating, int maxActors) {
    this.title = title;
    this.director = director;
    this.year = year;
    this.rating = rating;
    actors = new String[maxActors];
    numberOfActors = 0;
 }

   public String getTitle() {
     return title;
 }

  public String getDirector() {
    return director;
 }

  public int getYear() {
     return year;
 }

  public double getRating() {
     return rating;
 }

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

  public String [] getActors() {
     return Arrays.copyOf(actors, actors.length);
 }

  public boolean addActor(String actor) {
     if (numberOfActors < actors.length) {
        actors[numberOfActors] = actor;
        numberOfActors++;
     return true;
  }
   return false;
 }

 public String toString()  {
    StringBuffer sb = new StringBuffer();
    sb.append("Title: " + title + "\n");
    sb.append("Director: " + director + "\n");
    sb.append("Year: " + year + "\n");
    sb.append("Rating: " + rating + "\n");
    sb.append("Starring:\n");
   for (int i=0; i<numberOfActors; i++)
     sb.append("\t" + actors[i] + "\n");
   return sb.toString();
}


}

    public class Library {

  private Movie [] movies;
  private int numberOfMovies;

 public Library(int maxMovies) {
    movies = new Movie[maxMovies];
    numberOfMovies = 0;
 }

  public int getNumberOfMovies() {
     return numberOfMovies;
  }

  public boolean addMovie(Movie movie) {
     if (numberOfMovies < movies.length) {
        movies[numberOfMovies] = movie;
        numberOfMovies++;
     return true;
  }
   return false;
}

   public double averageRating() {
       double total = 0.0;
       for (int i=0; i<numberOfMovies; i++)
         total += movies[i].getRating();
        return total / numberOfMovies;
 }

  public void printLibrary() {
      for (int i=0; i<numberOfMovies; i++)
         System.out.println(movies[i]);
 }

}

我想知道,当我分别调用Class MovieUX的Switch Case#5和#6中的.printLibrary()和.averageRating()方法时,为什么不打印任何内容,因为它们嵌入在switch语句中,并且只要程序处理switch语句或其他原因,就不会存储信息?

谢谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

printLibrary() method替换为:

 public void printLibrary() {
     System.out.println(numberOfMovies);
     for (int i=0; i<numberOfMovies; i++)
         System.out.println(movies[i]);
 }

你会发现它输出:

0 

因此 for循环无法执行。和

averageRating()

相同