2015-11-14 57 views
0

我想在屏幕名稱旁邊顯示數組中的屏幕列表。如何在對象中顯示項目的數組列表?

這裏是Menu類

掃描儀輸入=新掃描儀(System.in)的代碼;

Screen [] screen; 


    public void menu() 
    { 


     screen[0] = new Screen("SRN-1",{"The Hunger Games: Mockingjay, Part 2", "Love the Coopers", "The 33"}, {6.78, 7.89, 5.75}, {21,23,26}); 
     screen[1] = new Screen("SRN-2",{"Kilo Two Bravo ", "Spectre (2015)", "The Peanuts Movie (2015)"}, {7.45,8.37,8.95}, {12,76,75}); 
     screen[2] = new Screen("SRN-3", {"Goosebumps (2015)", " Bridge of Spies (2015)", " Hotel Transylvania 2"}, {8.37,8.58,9.58}, {63,63,78}); 
     screen[3] = new Screen("SRN-4", {" The Last Witch Hunter (2015)", "Paranormal Activity: The Ghost Dimension (2015)", "The Intern (2015)"}, {5.76,7.37,9.47}, {63,84,13}); 

     prt(""); 
     prt("Select a screen: "); 

     for(int i = 0; i < screen.length; ++i){ 
      prt("Press " + i + " to select " + screen[i].getName(); 
     } 

     int inputNo = input.nextInt(); 

這裏是屏幕類的代碼。我的構造函數有什麼問題?

public class Screen { 

    String srnName; 
    String [] movieList; 

    double [] cost; 

    int [] seats; 

    public Screen(String srnName, String [] movieList, double [] cost, int [] seats) 

    {this.srnName = srnName; 
    this.cost = cost; 
    this.seats = seats; 
    this.movieList = movieList;} 

    public int[] getSeat() {return seats;} 
    public String getName() {return srnName;} 
    public String[] getMovie() {return movieList;} 
    public double[] getCost() {return cost;} 


} 
+0

究竟是什麼問題?你是否收到編譯錯誤?運行時錯誤?錯誤的結果? – Mureinik

+0

Stackoverflow不會運行你的代碼,它需要你的代碼和你試圖實現它的代碼面臨的問題是什麼。請讓它更清楚 – ihappyk

回答

0

1.必須定義一個尺寸陣列。

Screen[] screen = new Screen[4]; 

2.如果傳遞數組作爲參數必須添加類型:

screen[0] = new Screen("SRN-1", new String[]{"The Hunger Games: Mockingjay, Part 2", "Love the Coopers", "The 33"}, new double[]{6.78, 7.89, 5.75}, new int[]{21,23,26}); 

或者可以初始化變量作爲參數傳遞:

String[] movieList1 = {"The Hunger Games: Mockingjay, Part 2", "Love the Coopers", "The 33"}; 
double[] cost1 = {6.78, 7.89, 5.75}; 
int[] seats1 = {21,23,26}; 
screen[0] = new Screen("SRN-1", movieList1, cost1, seats1); 

3.您忘記了右括號:

prt("Press " + i + " to select " + screen[i].getName()); 

廣告1. 您可以使用列表,而不是OD陣列。那麼你將不會定義大小數組。

List<Screen> screen = new ArrayList<>(); 
screen.add(new Screen("SRN-1", new String[]{"The Hunger Games: Mockingjay, Part 2", "Love the Coopers", "The 33"}, new double[]{6.78, 7.89, 5.75}, new int[]{21,23,26})); 
// etc. you can add multiple objects 
相關問題