簡體   English   中英

第n個元素數組的控制台輸出

[英]Console output of nth element array

我有一個帶有n個元素的數組,我正在嘗試設置我的數組的值,以便每個元素的值都為Positon。

即,位置0處的第一個元素為0,位置2處的第二個元素,依此類推,直到位置n-1處的第n個元素,其值為n-1。

最后,我將在控制台上提供陣列的內容。

好吧,我已經設置了正確的值,但是我無法在控制台上顯示。例如,如何顯示位置“ n-1”的值為“ n-1”?

這是我到目前為止所做的:

  public void exercise1(Integer n){

   int[] arrayA = new int[n];

   int counter;

    for(counter=0; counter<arrayA.length; counter++){

              arrayA[counter]=counter;
                         }
    }   

提前致謝:)

使用System.out.println和以下代碼:

System.out.println(Arrays.    toString(your_array_name_here)); 

將此添加到您的循環

System.out.println(arrayA[counter]);

我認為HélioSantos所說的是對的,對我有用:

public class koponk {

 public static void main(String[] args) { exercise1(10); } public static void exercise1(Integer n) { int[] arrayA = new int[n]; int counter; for (counter = 0; counter < arrayA.length; counter++) { arrayA[counter] = counter; System.out.print(arrayA[counter] + " "); } } 

}

   public class apples{


public static void main(String[] args) {



    exercise(4);

}

   public void exercise(Integer n){



    int[] arrayA = new int[n];

    int counter;

    for(counter=0; counter<arrayA.length; counter++){

        arrayA[counter]=counter;
    }

    System.out.print("[");

    for(counter=0; counter<arrayA.length; counter++){

        if(counter == n-1){

         System.out.print(arrayA[counter]);   

        }else{


          System.out.print(arrayA[counter] + ", ");

       }
    } 
    System.out.println("]");





}

}

伙計們,在您提出建議后,我做了類似的事情,並且奏效了。 非常感謝你:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM