尝试使用循环从Array中检索索引

时间:2016-12-11 21:48:36

标签: java arrays loops

我制作了一个程序,用于存储电影名称,时间和显示时间。 我保存了输入到数组的详细信息,然后通过getter和setter方法将数组保存为记录。

在我的最终方法中,我尝试使用for循环打印出存储在记录中的详细信息,但不断出现错误。

对此问题的任何帮助将不胜感激。

//Demonstrates usage of loops/adt/gettersetters

import java.util.Scanner;

class movies {
    public static void main(String[] p) {
        Movie m = new Movie();

        int[] screenn = new int[4];
        int[] namee = new int[4];
        int[] hourr = new int[4];
        int[] minn = new int[4];

        for (int i = 0; i < 4; i++) {
            screenn[i] = i + 1;
            String moviename = input("Film for screen " + (i + 1));
            namee[i] = moviename;
            int moviehour = inputint("what hour does it start?");
            hourr[i] = moviehour;
            int moviemin = inputint("what min does it start?");
            minn[i] = moviemin;
        }
        sethour(m, namee);
        setmin(m, minn);
        setscreen(m, screen);
        setname(m, namee);

        showtime(m);
        System.exit(0);
    }

    //Getter Method
    public static String[] getname(Movie m) {
        return m.name;
    }

    public static int[] getscreen(Movie m) {
        return m.screen;
    }

    public static int[] gethour(Movie m) {
        return m.hour;
    }

    public static int[] getmin(Movie m) {
        return m.min;
    }

    //Setter Method
    public static Movie sethour(Movie m, int[] hour) {
        m.hour = hour;
        return m;
    }

    public static Movie setmin(Movie m, int[] min) {
        m.min = min;
        return m;
    }

    public static Movie setname(Movie m, String[] name) {
        m.name = name;
        return m;
    }

    public static Movie setscreen(Movie m, int[] screen) {
        m.screen = screen;
        return m;
    }

    public static String input(String message) {
        Scanner scanner = new Scanner(System.in);

        print(message);
        String answer = scanner.nextLine();
        return answer;
    }

    public static String print(String message) {
        System.out.println(message);
        return message;
    }

    public static int inputint(String message) {
        int number = Integer.parseInt(input(message));
        return number;
    }

    public static void showtime(movie m) {
        print("Cineworld Movies For Tonight");
        for (int i = 0; i < 4; i++) {
            print("");
            print(m.screen[i]);
            print(m.movie[i]);
            print(m.hour[i]);
            print(m.min[i]);
        }
    }
}

class Movie {
    String[] name = new String[4];
    int[] hour = new int[4];
    int[] min = new int[4];
    int[] screen = new int[4];
}

1 个答案:

答案 0 :(得分:0)

你的作业错了。根据OOP,Movie类应存储Movie的属性(名称,小时,分钟等),然后在MainProgram上存储一系列电影。如何创建和使用这些对象并不是它自己的类所关注的。话虽如此,我重新将你的mainProgram实现为:

  1. 询问每个(4)电影属性。
  2. 制作电影。
  3. 将其添加到电影列表中。
  4. 为每部电影(4)打印其属性。
  5. MovieClass:

    public class Movie
    {
        String name;
        int hour , min , screen;
    
        public Movie ( )
        {
            super ( );
        }
    
    
        public Movie ( String name , int hour , int min , int screen )
        {
            super ( );
            this.name = name;
            this.hour = hour;
            this.min = min;
            this.screen = screen;
        }
    
    
    
        public String getName ( )
        {
            return name;
        }
    
        public void setName ( String name )
        {
            this.name = name;
        }
    
        public int getHour ( )
        {
            return hour;
        }
    
        public void setHour ( int hour )
        {
            this.hour = hour;
        }
    
        public int getMin ( )
        {
            return min;
        }
    
        public void setMin ( int min )
        {
            this.min = min;
        }
    
        public int getScreen ( )
        {
            return screen;
        }
    
        public void setScreen ( int screen )
        {
            this.screen = screen;
        }
    
        @Override
        public String toString ( )
        {
            return "Movie [name="   + name + ", hour=" + hour + ", min=" + min + ", screen=" + screen
                    + "]";
        }
    
    
    
    }
    

    MainProgram(主程序):

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
        public class MainProgram
        {
    
            public static void main ( String [ ] args )
            {
                List < Movie > movieList = new ArrayList < Movie > ( );
    
                for(int i = 1; i <= 4; i++ )
                {
    
                    int screen = i;
                    String name = inputString ( "Film for screen "+(i));
                    int hour = inputInt ("what hour does it start?");
                    int min = inputInt ("what min does it start?");
    
                    Movie movie = new Movie ( name , hour , min , screen );
                    movieList.add ( movie );
                }
    
                print("Cineworld Movies For Tonight");
    
                for( Movie movie : movieList)
                {
                    print ( "" );
                    print ( Integer.toString ( movie.getScreen ( ) ));
                    print (movie.getName ( ));
                    print (Integer.toString ( movie.getHour ( ) ));
                    print ( Integer.toString ( movie.getMin ( ) ));
                }
    
            }
    
            public static String inputString ( String message )
            {
                Scanner scanner = new Scanner ( System.in );
                print ( message );
                String answer = scanner.nextLine ( );
                return answer;
            }
    
            public static String print ( String message )
            {
                System.out.println ( message );
                return message;
            }
    
            public static int inputInt ( String message )
            {
                int number = Integer.parseInt ( inputString ( message ) );
                return number;
            }
    
    
        }
    

    注意:正如您所看到的,我重新使用了一些静态方法,但如果您需要其他东西,则必须自己实施。

    I / O示例:

    Film for screen 1
    The Shawshank Redemption
    what hour does it start?
    19
    what min does it start?
    30
    Film for screen 2
    The Godfather
    what hour does it start?
    20
    what min does it start?
    15
    Film for screen 3
    The Godfather: Part II
    what hour does it start?
    20
    what min does it start?
    30
    Film for screen 4
    The Dark Knight
    what hour does it start?
    21
    what min does it start?
    15
    Cineworld Movies For Tonight
    
    1
    The Shawshank Redemption
    19
    30
    
    2
    The Godfather
    20
    15
    
    3
    The Godfather: Part II
    20
    30
    
    4
    The Dark Knight
    21
    15
    

    希望它有所帮助。