JAVA:替换数组中的数字,数字位置并打印出来

时间:2016-09-17 14:38:39

标签: java arrays replace while-loop

我这里有一个小问题。我想用数字位置替换数组中的所有负数。我的问题是在替换数字之前打印出数组,并且我想在替换之后打印出数组... 这是我的代码:

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设置,其中负数被数组中的位置替换,你将获得打印出的负数的位置,以及有多少负数。 我希望你能帮帮我! :)谢谢!

3 个答案:

答案 0 :(得分:0)

您的代码中有太多冗余条件。您的尝试非常接近,您可以简单地执行以下操作:

while (counter < heltall.length) {
    // if the number is negative, replace it with its index
    if (heltall[counter] < 0) {
        heltall[counter] = counter;
    }
    counter++;
}
// outside the loop
System.out.println(Arrays.toString(heltall));

重要提示:在这种情况下,您应该调试您的代码。这有助于您更好地了解代码流,并发现您不了解的问题。我强烈建议您调试当前的代码,然后再尝试修复它。

答案 1 :(得分:0)

您不需要那么多条件来取代负数。当你在循环中得到一个负数时,只需逐个替换。

删除冗余:有一点需要记住:当您有相同条件的某些陈述时,您不需要单独进行。将所有语句写在相同的if块中。

例如,在您的代码中:

if (heltall[counter] < 0) {
    System.out.println(heltall[counter]);
}

if (heltall[counter] < 0) {
    heltall[counter] = counter;
}

if (heltall[counter] < 0) {
    sumNeg++;
}

if (heltall[counter] < 0) {
    System.out.println("Negative numbers position in array is : " + counter);
}

可以替换为:

if(heltall[counter] < 0) { // do all in the same if condition block
    System.out.println(heltall[counter]);
    heltall[counter] = counter;
    sumNeg++;
    System.out.println("Negative numbers position in array is : " + counter);
}

解决方案无论如何,整个代码可能如下所示:

while (counter < heltall.length) {
    // replacing negative numbers
    if (heltall[counter] < 0) {
        heltall[counter] = counter;
        sumNeg++;
    }
    counter++;
}
System.out.println("There were : " + sumNeg + " negative numbers.");
System.out.println("Array after replacing negative numbers: "+Arrays.toString(heltall));

答案 2 :(得分:0)

以下是您的代码的工作版本:

注意:更换后必须放置打印命令。代码按行顺序逐行运行。顶行的语句首先运行,然后运行较低的行(确保按顺序)。

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){

    if(heltall[counter] < 0 ){

      //replacing negative numbers
      heltall[counter]=counter;

      //counting negative number amount
      sumNeg++;

      //array print out after replace
       System.out.println("array[" + counter + "] = " + heltall[counter]);

      //negative numbers position print out
      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.");


    }
  }