我在這裏有一個小問題。我想用數字位置替換數組中的所有負數。我的問題是,數組被打印出來,被替換的數字面前,我想打印出數組就被替換後... 這裏是我的代碼:JAVA:使用數字位置替換數組中的數字並打印出來
public class oppgave33{
public static void main(String[] args) {
int[] heltall = {1, 4, 5, -2, -4, 6, 10, 3, -2};
int counter = 0;
int sumNeg = 0;
while(counter < heltall.length){
//array print out
System.out.println("array[" + counter + "] = " + heltall[counter]);
if(heltall[counter] < 0){
System.out.println(heltall[counter]);
}
//replacing negative numbers
if(heltall[counter] < 0){
heltall[counter]=counter;
}
if(heltall[counter] < 0){
sumNeg++;
}
//negative numbers position print out
if(heltall[counter] < 0){
System.out.println("Negative numbers position in array is : " + counter);
}
counter++;
}
//printing out how many negative numbers
System.out.println("There are : " + sumNeg + " negative numbers.");
}
}
最後一點:如果你刪除在if設置中,負數由陣列中的位置替換,您將獲得打印出負數的位置,以及有多少負數。 我希望你能幫助我! :) 謝謝!
可能重複的http://stackoverflow.com/questions/39528556/replacing-array-values,但這次是從提問者的努力。 :-) –