如何打印二维数组的区间

How to print an interval of a 2D Array

所以我用文件中的数据初始化了我的 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];
}

我的数组基本都是Temperature,每一行都是某个日期的温度,都是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];

其中闰年是一种计算二月是否有 28 天或 29 天的方法

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

你的问题一定是,简单地做两个嵌套循环,并不能为你解决这个问题。相反,您想将日历从 "start month" 和 "start day" 递增到 "end month" 和 "end day"。

更新

好的,由于您不想为此使用日历,因此您可以通过使用一些逻辑遍历二维数组来完成此操作。我假设数组的第二个维度的长度会根据我们所处的月份而有所不同。意思是,提供数据的代码在每个有效日期只有一个条目。

因此,如果数据是在闰年记录的,则二月的数组维度的长度将为 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
    }
}

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

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


for(int i=m1+1; i < m2; i++) {
    for(int j=j1+1; j < j2; j++) {
         System.out.printf("Temperature on day %d and month %d: %s", j, i,  TableauLectureFichier[i][j]);
    }
}

}