我在这里有一个小问题。我想用数字位置替换数组中的所有负数。我的问题是,数组被打印出来,被替换的数字面前,我想打印出数组就被替换后... 这里是我的代码: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,但这次是从提问者的努力。 :-) –