特别环路

问题描述:

嘿,我希望做一个循环,借此数组中:特别环路

String[] arr = new String[3]; 
arr[0] = "one "; 
arr[1] = "two "; 
arr[2] = "three "; 

和输出:

one two three one one two two three three one one one two two two three three three 

从本质上讲,我希望它增加它显示每个时代数组值每次迭代一次。

+0

这听起来像一个家庭作业的问题...尝试Math.floor – Drew 2010-12-09 07:03:18

+0

玩,我觉得你的意思是 “新的String [3];” – Gopi 2010-12-09 07:06:18

for (int i=1; i<=n; i++) { 
    for (int j=0; j<3; j++) { 
     for (int k=0; k<i; k++) { 
      System.out.print(arr[j] + " "); 
     } 
    } 
} 

该代码将导致异常:您正在声明2个位置的数组并分配3个值。 arr [2]会导致异常(超出范围)。

使用嵌套循环打印内容。

for(int i=0;i<Array.length;i++) 
     { 
      for(int j= 0 ;j<Array.length;j++) 
       { 
        for(int k=0;k<=i;k++) 
         { 
         System.out.println(""+Array[j]); 
         } 
       } 
     } 

另一种方法:

String[] array = {"One ","Two ", "Three "}; 
int n = 10; 
int counter = 1; 

     for(int i =0;i<n;i++){ 
      for(int j=0;j<array.length;j++){ 
       String element = array[j]; 
       for(int k=0;k<counter;k++){ 
        System.out.print(element+" "); 
       } 
      } 
      counter++; 
      System.out.println(); 
     }