Java 如何打印二维数组的间隔
所以我用一个文件中的数据初始化了我的2D数组,我想知道是否可以在这个时间间隔内打印System.out.printJava 如何打印二维数组的间隔,java,arrays,2d,Java,Arrays,2d,所以我用一个文件中的数据初始化了我的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 MON
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];
}
其中,LeapYear是一种计算二月有28天还是29天的方法,我想您希望从传递给函数的二维数组中打印,并使用用户的输入从传递的数组中打印选定范围 您的问题一定是,仅仅做两个嵌套循环并不能解决这个问题。相反,您希望将日历从“开始月”和“开始日”增加到“结束月”和“结束日” 更新 好的,因为您不想为此使用日历,所以您可以通过使用一点逻辑来迭代2d数组来实现这一点。我将假设数组的第二维度的长度将根据我们所处的月份而变化。也就是说,提供数据的代码对于每个有效日期只有一个条目 因此,如果数据是在闰年记录的,那么2月份的数组维度的长度将仅为29。(
表格[1]。长度==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
}
}
publicstaticvoidoptionOn(字符串[][]tableUChier){
扫描仪rDates=新扫描仪(System.in);
System.out.println(“按以下顺序在此处输入时间间隔DAY1-MONTH1 DAY2-MONTH2”);
字符串Intervale=rDates.nextLine();
字符串[]TabChaine=Intervale.split(“”);
int[]tIntervale=新int[4];
对于(int i=0;ipublicstaticvoidoptionon(字符串[][]表){
Scanner rDates=新的扫描仪(System.in);
System.out.println(“按以下顺序在此处输入时间间隔DAY1-MONTH1 DAY2-MONTH2”);
字符串Intervale=rDates.nextLine();
字符串[]TabChaine=Intervale.split(“”);
int[]tIntervale=新int[4];
对于(int i=0;i数组tableaulucturefichier
包含什么?您是否询问如何打印您拥有的整数数组的元素?或者您是否计划使用int j1、j2、m1和m2索引到您在方法头中接收的数组中?有关您尝试执行的操作的更多详细信息,您面临的问题是什么s、 到目前为止,您所做的尝试将帮助您快速获得更好的答案您需要详细说明“打印”的含义你想要什么输出。java为你提供了许多设置字符串格式的选项。是的,我刚刚意识到我忘记了指定年份,但我的程序读取了文件中的所有数据,所以这取决于用户将输入什么文件,以及该文件与什么年份相关,所以我必须让它在闰年也能工作。问题是,这是我的学校项目d我们以前从未使用过日历,因此我不能用它来打印2D阵列的该部分。这就是为什么我想知道在用户输入的开始日期/月份和结束日期/月份之间,是否有任何循环可以只打印阵列的一部分。我想我现在理解了您的请求。请检查上面的代码-并测试这是否适合你。是的,我花了很长时间试图找出正确的循环。这一个工作非常好。谢谢你的帮助。
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
}
}
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]);
}
}