所以我一直在這個相同的例子問題一個星期了。我知道它可能似乎很容易,但我發現我越看它或改變它,越多 困惑,我得到。我覺得我要讓這個難度比 需要更多。我加載的數組在Try-Catch
部分中正確顯示,但我需要用它自己的方法顯示它,我調用listOfAges()
。這看起來像什麼?任何反應表示讚賞,請幫助。循環陣列讓我變傻
class ArrayDemo {
public static void main(String[] args) {
int[] anArray;
int ageCount = 0;
int age;
String filename = "ageData.dat";
anArray = new int[50];
/* The file has 50 numbers which represent an employee's age */
try {
Scanner infile = new Scanner(new FileInputStream(filename));
while (infile.hasNext()) {
age = infile.nextInt();
ageCount += 1;
System.out.println(ageCount + ". " + age + "\n");
/*
* When i run just this, the output is correct...
*
* But i don't want the output here, i just want to gather
* the information from the file and place it at the bottom inside
* the method displayAges().*/
}
infile.close();
} catch (IOException ex) {
ageCount = -1;
ex.printStackTrace();
}
public void listOfAges() {
System.out.print(" I want to display the data gathered from ageData.dat in this method ");
System.out.print(" Also, i receive this error message when i try: 'Illegal modifier for parameter listOfAges; only final is permitted' ")
}
}
}
這些例子正是我一直在努力的,非常有幫助的人。非常感謝 –