简体   繁体   English

尝试使用循环从数组中检索索引

[英]Trying to retrieve index from Array using loop

I made a program which stores movie names, hour and time of showing. 我制作了一个程序,用于存储电影名称,放映时间和时间。 I saved the details inputted to arrays and then saved the arrays by getter and setter methods as a record. 我保存了输入到数组的详细信息,然后通过getter和setter方法将数组保存为记录。

In my final method i attempt to use a for loop to print out the details stored in the records but constantly get errors. 在我的最后一种方法中,我尝试使用for循环打印出记录中存储的详细信息,但不断出错。

Any help to where the issue is would be greatly appreciated. 如有任何问题的帮助,将不胜感激。

//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];
}

You are getting your assignment wrong. 您的作业错了。 According to OOP The Movie class should store Movie's properties (name, hour, mins, etc) and then on your MainProgram you should store an array of movies. 根据OOP,Movie类应存储Movie的属性(名称,小时,分钟等),然后在MainProgram上应存储电影数组。 How you create and the usage of these objects is not concern of the class it self. 如何创建和使用这些对象与它自己的类无关。 Having said that i re-implemented your mainProgram to: 话虽如此,我重新实现了您的mainProgram到:

  1. Ask for each (4) movie attributes. 询问每个(4)电影属性。
  2. Create the movie. 创建电影。
  3. add it to a list of movies. 将其添加到电影列表中。
  4. for each movie (4) print its properties. 为每个电影(4)打印其属性。

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;
        }


    }

Note: as you can see i re-used some of your static methods but if you need something else you have to implement it by yourself. 注意:如您所见,我重复使用了一些静态方法,但是如果您需要其他方法,则必须自己实现。

I/O Example: 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

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从数据库中检索多个记录,并将它们添加到JSON数组中。 但是对于每个循环json数组都被清除了 - Trying to retrieve multiple records from db and add those to JSON array. but for every loop json array is getting cleared 使用索引从Map检索值 - Retrieve values from the Map using the index 使用for循环计算数组中索引的数量 - Counting number of index in array using for loop 使用for循环从数据库检索数据 - Data Retrieve from Database using for loop 尝试创建使用另一个数组+哈希图的信息填充数组的循环 - Trying to create loop that fills array using info from another array + hash map 使用for循环在字符串数组中打印索引号和该索引中的字符 - Using a for loop to print index number and the character in that index in a string array 是否可以在不使用任何索引值和特定键的情况下从 json object 中的 json 数组中检索和打印数据 - is it possible to retrieve and print the data from a json object inside json array without using any index values and specific keys 我只是想在不使用数组的情况下打印循环中的最大数字 - I am just trying to print the greatest number from the loop without using an array 使用Jackson从JSON数组中检索字符串 - Retrieve strings from JSON array using Jackson 使用split()数组索引超出范围但循环读取数组后 - After using split() Array Index Out Of Bounds but reads array in loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM