错误数组输出

时间:2016-04-02 03:15:32

标签: java for-loop

每当我尝试为我的数组创建一个方法时,我在包含的行上都会出错 (int i =0; i < list.length; i++)list.length下会显示一条红线,但是当我没有将其放在方法下时,就没有错误。我该如何解决?我需要使用一种方法。

public static void main(String[] args)

{
    System.out.println("Sergio Borja, Class Meeting Time: Tue/Thu 1:30-2:50pm");
    Movie[] list = new Movie[6];
    list[0] = new Animated("Batman Begins", "Zack Snyder" , 2006, 4000000000.0, 4, 3000.0);
    list[1] = new Animated("Batman v Superman", "Jack Smith", 2016, 250200.0, 3, 20000.0);
    list[2] = new Documentary("The Lone Ranger", "Spike Lee", 2002, 2000000.0, 1000, 4000.0);
    list[3] = new Documentary("Cowboys", "Bob lies", 1992, 5000000.0, 1050 , 2000.0);
    list[4] = new Drama ("Karate Kid","Jackie Chan", 1998, 2000000000.0, 60000000.0, 15.25);
    list[5] = new Drama ("The Amazing SpiderMan", "Bobby Smith", 2014, 150000000.0, 20000000.0, 14.50);

}
    public void printMovieInfo(Movie[] Movie)
    {
        for (int i =0; i < list.length; i++){
            System.out.printf("%s", list[i].toString());
    }
}

3 个答案:

答案 0 :(得分:1)

您传入名为Movie的数组(也是class的名称),但尝试使用名为list的数组。变化

public void printMovieInfo(Movie[] Movie)

public void printMovieInfo(Movie[] list)

答案 1 :(得分:0)

printMovieInfo不知道list。将其替换为正确的参数名称,即Movie(并在工作后用小写重命名)。

答案 2 :(得分:0)

您的 list 变量位于main方法中,printMovieInfo方法无法看到该方法。使用你的论证中的电影变量。另外,从主要添加printMovieInfo调用。

public static void main(String[] args)

{
    System.out.println("Sergio Borja, Class Meeting Time: Tue/Thu 1:30-2:50pm");
    Movie[] list = new Movie[6];
    list[0] = new Animated("Batman Begins", "Zack Snyder" , 2006, 4000000000.0, 4, 3000.0);
    list[1] = new Animated("Batman v Superman", "Jack Smith", 2016, 250200.0, 3, 20000.0);
    list[2] = new Documentary("The Lone Ranger", "Spike Lee", 2002, 2000000.0, 1000, 4000.0);
    list[3] = new Documentary("Cowboys", "Bob lies", 1992, 5000000.0, 1050 , 2000.0);
    list[4] = new Drama ("Karate Kid","Jackie Chan", 1998, 2000000000.0, 60000000.0, 15.25);
    list[5] = new Drama ("The Amazing SpiderMan", "Bobby Smith", 2014, 150000000.0, 20000000.0, 14.50);

    printMovieInfo(list);  // <--- here is the call to print movie info

}

public static void printMovieInfo(Movie[] movie)
{
        for (int i =0; i < movie.length; i++){
            System.out.printf("%s", movie[i].toString());
}