我必須從外部文件讀取數字列表並將它們插入到它們自己的數組中,以獲得正數或負數。掃描器讀取的數字很好,但是當它將它們插入到數組中時就是出錯的地方。正如你在下面看到的是我的代碼和輸出。輸出中打印的是文件中的內容,但當我要求它打印數組時,這是混亂的字母/數字/符號。任何人都可以幫我修復它嗎?可能的編碼錯誤[I @ 24e11c
public class Numbers {
public static void main(String[] args) throws IOException {
Scanner reader = new Scanner(new FileInputStream("First.dat"));
int Positive[] = new int[20];
int Negative[] = new int[20];
int X = 0;
int Y = 0;
while (reader.hasNextInt()) {
int Start = reader.nextInt();
System.out.println(Start);
if (Start < 0) {
Negative[X] = Start;
X += 1;
}
if (Start > 0) {
Positive[Y] = Start;
Y += 1;
}
}
System.out.println(Positive + " " + Negative);
}
}
輸出:
3
66
54
-8
22
-16
-56
19
21
34
-34
-22
-55
-3
-55
-76
64
55
9
39
54
33
-45
[[email protected] [[email protected]
您希望打印什麼?爲什麼? –
我希望它打印出數字,我將數字插入 –
如果你不知道自己在做什麼,那麼_Want_是無關緊要的。你做了什麼,你爲什麼這樣做?你爲什麼不做別的事? –