首页 > 解决方案 > 如何打印二维数组的间隔

问题描述

所以我用文件中的数据初始化了我的 2D 数组,我想知道是否有可能 System.out.print 只是这个间隔。

public static void OptionUn (String[][] TableauLectureFichier) {

    Scanner rDates = new Scanner(System.in);

    System.out.println("ENTER YOUR INTERVAL HERE IN THE FOLLOWING ORDER DAY1 MONTH1 DAY2 MONTH2");

    String Intervale = rDates.nextLine();

    String[] TabChaine = Intervale.split(" ");
    int[] tIntervale = new int[4];

    for (int i = 0; i <TabChaine.length; i++) {            
        tIntervale[i] = Integer.parseInt(TabChaine[i]);
    }

    int j1 = tIntervale[0],
        j2 = tIntervale[2],
        m1 = tIntervale[1],
        m2 = tIntervale[3];
}

我的数组基本上包含温度,每一行是某个日期的温度,它的全部订购时间为 1 月 1 日至 12 月 31 日。我的问题是我怎样才能只 System.out.print 用户输入日期之间的温度。例如 1 月 1 日至 2 月 1 日之间 (1 1 1 2)。

所以数组看起来如下。这是一个不规则的二维数组

String[][] TableauLectureFichier = new String[12][];
        TableauLectureFichier[1] = new String[31];
        TableauLectureFichier[2] = new String[LeapYear];
        TableauLectureFichier[3] = new String[31];
        TableauLectureFichier[4] = new String[30];
        TableauLectureFichier[5] = new String[31];
        TableauLectureFichier[6] = new String[30];
        TableauLectureFichier[7] = new String[31];
        TableauLectureFichier[8] = new String[31];
        TableauLectureFichier[9] = new String[30];
        TableauLectureFichier[10] = new String[31];
        TableauLectureFichier[11] = new String[30];
        TableauLectureFichier[12] = new String[31];

LeapYear 是一种计算 2 月是否有 28 天或 29 天的方法

标签: javaarrays2d

解决方案


我想你想从传递给函数的二维数组中打印,并使用用户的输入从传递的数组中打印选定的范围?

你的问题一定是,简单地做两个嵌套循环,不会为你解决这个问题。相反,您希望将日历从“开始月份”和“开始日期”增加到“结束月份”和“结束日期”。

更新

好的,因为您不想为此使用日历,您可以通过使用一些逻辑迭代 2d 数组来做到这一点。我将假设数组第二维的长度会根据我们所处的月份而有所不同。意思是,提供数据的代码对于每个有效日期只有一个条目。

因此,如果数据是在闰年记录的,那么 2 月的数组维度将只是长度为 29。 ( TableauLectureFichier[1].length == 29)

然后你可以用以下方法做到这一点:

public static void OptionUn (String[][] TableauLectureFichier) {

    Scanner rDates = new Scanner(System.in);

    System.out.println("ENTER YOUR INTERVAL HERE IN THE FOLLOWING ORDER DAY1 MONTH1 DAY2 MONTH2");

    String Intervale = rDates.nextLine();

    String[] TabChaine = Intervale.split(" ");
    int[] tIntervale = new int[4];

    for (int i = 0; i <TabChaine.length; i++) {            
        tIntervale[i] = Integer.parseInt(TabChaine[i]);
    }

    int j1 = tIntervale[0],
        j2 = tIntervale[2],
        m1 = tIntervale[1],
        m2 = tIntervale[3];

    // Loop through all the months specified, taking care to convert from base 1 
    // to base 0
    for (int month = m1 - 1; month < m2; month++) {
        // Assume that we will start at the first day of a month
        int day = 0;

        // However, if the current month is the first, start from the day specified as j1 
        // instead, and convert to base 0 so that we can use it to index into the array
        if (month == m1) {
            day = j1 - 1;
        }

        // Assume that we will run till the end of this month, or in this case to the end
        // of the array for the current month
        int endDay = TableauLectureFichier[month].length;

        // If the current month is the last month, only run to specified end day
        if (month == m2) {
            endDay = j2;
        }

        // Now run from the start day to the end day in the current month, paying 
        // attention to that endDay is still base 1
        for (; day < endDay; day++) {
            System.out.println(String.format("Month: %d, day: %d, value %s",
                month, 
                day, 
                TableauLectureFichier[month][day]));
        }

        // At this point we will step into the next month
    }
}

我没有向monthor 'day' 变量添加边界检查。因此,如果您将其交给任何人 - 请在使用数组中的变量之前添加边界检查。


推荐阅读