[英]Java program not outputting negative integers from array?
我的Java任務之一是從文件中獲取數字,然后將其分離為兩個數組。 一個名為P(正數)和N(負數)的設備。我讓它適用於正數,但負數始終輸出0。 我不知道為什么! 救命?
import java.io.*;
import java.util.*;
public class Prog404a {
public static void main(String[] args) {
Scanner inFile = null;
try {
inFile = new Scanner(new File("prg404a1.dat"));
} catch (FileNotFoundException e) {
System.out.println("File not found!!");
System.exit(0);
}
int temp = 0;
int P[] = new int[23];
int N[] = new int[23];
int i = 0;
while (inFile.hasNext()) {
temp = inFile.nextInt();
if (temp < 0) {
N[i] = temp;
}
if (temp > 0) {
P[i] = temp;
}
i++;
}
for (int x = 0; x < i; x++) {
System.out.println(P[x] + "\t" + N[x]);
}
}
}
編輯:沒關系,它也不適用於正數。 只有一點。
也許您只是在數數不對?
您應該使用兩個計數器,一個用於正數,一個用於負數。 否則,條目的一半顯然將為0,因為它們從未設置過 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.