我正在尝试使用for循环更新以前声明的变量,并且在循环内,变量值正在更新(使用print语句进行检查)。但是,在循环结束之后,如果我在循环外使用打印语句检查这些值,则它们与循环前的值相同,并且没有更新供我在其他地方使用。
public class Intervals {
public static void main(String[] args) {
// Declaring necessary constants
int MINUTES_IN_DAY = 1440;
int MINUTES_IN_HOUR = 60;
// Take user inputs for interval start and end times in hours
Scanner input = new Scanner(System.in);
System.out.print("Enter the earlier interval's start and end time in 24-hour time format. ");
int intervalStart1 = input.nextInt();
int intervalEnd1 = input.nextInt();
System.out.print("Enter the later interval's start and end time in 24-hour time format. ");
int intervalStart2 = input.nextInt();
int intervalEnd2 = input.nextInt();
// For-each loop that converts all 24-hour times to minutes after midnight
int times[] = {intervalStart1, intervalEnd1, intervalStart2, intervalEnd2};
for (int i: times) {
i = (i / 100 * MINUTES_IN_HOUR) + (i % 100);
System.out.println("the interval is " + i);
}
// ERROR: values from for loop are not being saved, so variable values are not being updated as shown in next print line.
System.out.println(intervalStart1);
答案 0 :(得分:0)
int times[] = {intervalStart1, intervalEnd1, intervalStart2, intervalEnd2};
for (int i: times) {
i = (i / 100 * MINUTES_IN_HOUR) + (i % 100);
System.out.println("the interval is " + i);
}
i
是数组中值的副本,它不像指针,更新i
仅会更新副本,而该副本将在下一次循环迭代时丢弃。 / p>
答案 1 :(得分:0)
您做出两个错误的假设。
将值分配给增强的for循环的循环变量不会更改您要遍历的数组(或Iterable
)的相应元素。您的循环等效于:
for (int index = 0; index < times.length; index++) {
int i = times[index];
i = (i / 100 * MINUTES_IN_HOUR) + (i % 100);
System.out.println("the interval is " + i);
}
显然不会更新数组中的值。为了修改数组,您必须编写:
for (int index = 0; index < times.length; index++) {
times[index] = (times[index] / 100 * MINUTES_IN_HOUR) + (times[index] % 100);
System.out.println("the interval is " + times[index]);
}
您的数组包含从其他变量(例如intervalStart1
)初始化的值。即使您更改了数组中的值(您没有这样做),也不会更改原始变量的值。
答案 2 :(得分:0)
您不需要更新它们,在获得值i
之后,它将不再引用数组中的值。这是副本。
int times[] = {intervalStart1, intervalEnd1, intervalStart2, intervalEnd2};
for (int i: times) {
i = (i / 100 * MINUTES_IN_HOUR) + (i % 100);
System.out.println("the interval is " + i);
}
要更新,您必须在循环中修改它们,请使用一些index
变量来访问元素并更新它们
int index=0;
int times[] = {intervalStart1, intervalEnd1, intervalStart2, intervalEnd2};
for (int i: times) {
times[index++] = (i / 100 * MINUTES_IN_HOUR) + (i % 100);
System.out.println("the interval is " + i);
}
System.out.println(times[0]);
答案 3 :(得分:0)
// For-each loop that converts all 24-hour times to minutes after midnight
int times[] = {intervalStart1, intervalEnd1, intervalStart2, intervalEnd2};
for (int i: times) {
i = (i / 100 * MINUTES_IN_HOUR) + (i % 100);
System.out.println("the interval is " + i);
}
// ERROR: values from for loop are not being saved, so variable values are not being updated as shown in next print line.
System.out.println(intervalStart1);`
我认为您想更新times []数组的值吗?
因此,如果您要更新此内容,请使用常规循环执行1件事,即
for(int i =0; i < times.length;i++){
times[i] = (times[i]/100*MINUTES_IN_HOUR) + (I % 100);
System.out.println("the interval is "+times[i]);
}
System.out.println(times[0]);`
但是,如果要更新
,请考虑一件事。intervalStart1
变量,则必须在循环后从times数组分配此值。
我希望它对您有用。