我正在尋找解決方案,以找到在陣列負數,我想出了像搜索這樣的代碼。計算負數陣列中的
public static void main(String args[]){
int arrayNumbers[] = { 3, 4, 7, -3, -2};
for (int i = 0; i <= arrayNumbers.length; i++){
int negativeCount = 0;
if (arrayNumbers[i] >= 0){
negativeCount++;
}
System.out.println(negativeCount);
}
}
我想知道是否有更容易或更短的方式來找到數組中的負數與上面的代碼?
這是計數'> = 0',而不是負數。 – assylias
由於'for'中的終止條件,該代碼將生成越界異常。 – hmjd
該代碼根本無法編譯,您無法訪問for循環之外的negativeCount。 – josefx