-1
我試圖創建一個數組,可以讓一部電影不能在每個數組索引來初始化對象
public void addMovie() {
for(int x = 0; x < mlist.length; x++) {
mlist[x] = new Movies();
System.out.println("What is the title of the movie? ");
title = scan.nextLine();
System.out.println("What is the genre of the movie? ");
genre = scan.nextLine();
System.out.println("Who is the director of the movie? ");
director = scan.nextLine();
System.out.println("What is the cost of the movie? ");
cost = scan.nextInt();
}
}
當我編譯用戶輸入的信息,它說,
mlist[x] = new Movies();
構造函數類Movies中的電影不能應用於給定的類型;
完整代碼:
import java.util.Scanner;
public class Movies
{
private String title, genre, director;
private int cost;
Movies mlist[] = new Movies[5];
Scanner scan = new Scanner(System.in);
public Movies(String mtitle, String mgenre, String mdirector, int mcost)
{
title = mtitle;
genre = mgenre;
director = mdirector;
cost = mcost;
}
public void addMovie()
{
for(int x = 0; x < mlist.length; x++)
{
mlist[x] = new Movies();
System.out.println("What is the title of the movie? ");
title = scan.nextLine();
System.out.println("What is the genre of the movie? ");
genre = scan.nextLine();
System.out.println("Who is the director of the movie? ");
director = scan.nextLine();
System.out.println("What is the cost of the movie? ");
cost = scan.nextInt();
}
}
public String getTitle()
{
return title;
}
public String getGenre()
{
return genre;
}
public String getDirector()
{
return director;
}
public int getCost()
{
return cost;
}
}
「電影」的構造函數是什麼,「mlist」的類型是什麼? –
您正在使用Movies的默認構造函數,因爲您定義了自己的(獲取更多參數),因此不再能夠使用它。你可以讀入這些變量,然後調用你自己的構造函數傳入這些值。 – chatton
你也有一個嚴重的設計問題。爲什麼電影有5部電影。如果一家電影院有5部電影,我會理解。但電影沒有電影。爲什麼它被稱爲「電影」而不是「電影」? –