如何在Java中打印數組的索引? 我真的堅持了這一點,我覺得它應該是這樣的if語句像在Java中打印數組的索引
if (i == 5050) {
System.out.println("Index is: " +i);
}
我會很感激,即使這只是開始尋找答案的好地方任何幫助。 感謝
public class summation {
public static void main(String[] args) {
long[] a = new long[101];
long sum;
int i, numbers;
numbers = 100;
// initialise the array a using the loop counter
for (i = 1; i <= numbers; i++) {
a[i] = (long) i;
}
sum = 0;
for (i = 1; i <= numbers; i++) {
// do summation
sum = sum + a[i];
}
System.out.println("sum of numbers between 1 and " + numbers + " is " + sum);
}
}
你想在哪裏打印索引,以及索引?你的問題很不清楚。 – Taylor 2014-10-09 14:19:55
Java數組索引從0開始。另外,1到100之間的數字如何等於5050? – 2014-10-09 14:20:38
我想打印數字5050的索引,遺憾的是缺乏清晰度。 @ElliotFrisch我將1-100的總和加起來就是5050. – 2014-10-09 14:21:41