2013-04-26 61 views
-3

如何循環?如何打印大於0的連續整數,每行三個?

我想這個循環:

0-> 1,2,3 
1-> 4,5,6 
2-> 7,8,9 
3-> 10,11,12 
4->..... 
...... 

我不知道如何寫這個算法。 我在下面嘗試,它不起作用。

public class gYie { 

    public static void main(String[] args) { 
     int current = 0; 
     int death = 0; 
     for (int i = 0; i < 10; i++) { 
      System.out.print(i + " "); 
      for (int j = 0; j < 3; j++) { 
       System.out.print(death+j +" "); 

       current += j; 
      } 
      death += current; 
      System.out.println(""); 
     } 
    } 
} 

其輸出是:

run: 
0 0 1 2 
1 3 4 5 
2 9 10 11 
3 18 19 20 
4 30 31 32 
5 45 46 47 
6 63 64 65 
7 84 85 86 
8 108 109 110 
9 135 136 137 

如何解決這個問題?我想不出如何寫它。
3變爲18,19,20而不是12,13,14。

+1

輸出應該是什麼樣子? – CloudyMarble 2013-04-26 05:04:55

+0

檢查我的答案,只使用一個循環的解決方案 – rajesh 2013-04-26 05:16:41

回答

0

你需要做這樣的事情。只需繼續打印current

int current = 1; 
    for (int i = 0; i < 10; i++) { 
     System.out.print(i + " "); 
     for (int j = 0; j < 3; j++) { 
      System.out.print(current +" "); 
      current ++; 
     } 
     System.out.println(); 
    } 
2

你正在推翻它。對於左側,您需要打印的是i。對於右手邊,你只需要一個變量current是被遞增每次打印的:

int current = 1; 
for (int i = 0; i < 10; i++) { 
    System.out.print(i + " "); 
    for (int j = 0; j < 3; j++) { 
     System.out.print(current + " "); 

     current ++; 
    } 
    System.out.println(); 
} 
} 
2

看起來很像功課,所以這裏的一些僞代碼(實際上Python)的,會做的伎倆爲您提供:

for outer in range (10): 
    print "%d ->"%(outer), 
    for inner in range (3): 
     print "%2d"%(outer * 3 + inner + 1), 
    print 

的基本思想是通過簡單地具有0內環2以內,外循環每次增加1次。然後公式:

outer * 3 + inner + 1 

給你你想要的值:

0 -> 1 2 3 
1 -> 4 5 6 
2 -> 7 8 9 
3 -> 10 11 12 
4 -> 13 14 15 
5 -> 16 17 18 
6 -> 19 20 21 
7 -> 22 23 24 
8 -> 25 26 27 
9 -> 28 29 30 
0

您可以直接簡化代碼如下:

public static void main(String[] args) { 
    int death = 3; 
    for (int i = 0; i < 10; i++) { 
     System.out.print(i + " "); 
     death = 3*i; 
     for (int j = 1; j <= 3; j++) { 

      System.out.print(death+j +" "); 
     } 
     System.out.println(""); 
    } 
} 

}

現在,您將得到輸出爲: -

0 1 2 3 
1 4 5 6 
2 7 8 9 
3 10 11 12 
4 13 14 15 
5 16 17 18 
6 19 20 21 
7 22 23 24 
8 25 26 27 
9 28 29 30 
0

本應該做的工作......

public class gYie { 

    public static void main(String[] args) { 
     for (int i = 0, j = 1; i < 10; i++) { 
      System.out.print(i + " "); 
      for (int h = 0; h < 3; h++, j++) { 
       System.out.print(j +" "); 
      } 
      System.out.println(""); 
     } 
    } 
} 
-1

更改代碼這樣。

int death = 1; 
for (int i = 1; i <= 10; i++) { 
System.out.print(i + " "); 
for (int j = 0; j < 3; j++) { 
    System.out.print(death++ +" "); 

    //current += j; 
    } 
    //death += current; 
    System.out.println(""); 
} 
2

試試這個

int loopCount = 1; 

for(int a = 1; a < 21; a++){ 
    System.out.println(a); 
    for(int b = 0; b < 3; b++){ 
     System.out.print((loopCount++) + " "); 
    } 
    System.out.println(); 
} 

編輯:但我想我發現了一個更有效的方式使用單個環路

int x = 1; 

for(int a = 0; a < 21; a++){  
    System.out.println(a + " -> " + (x) + " " + (x + 1) + " " + (x + 2)); 
    x = x + 3; 
} 

現在你可以用你的變量進行合併和邏輯

0

使用一個簡單的計數器:

int j = 0; 
for (int i = 0; i < 10; i++) 
    System.out.println(i + " " + ++j + " " + ++j + " " + ++j + " "); 
-1

得到確切的解決方案,這...

class Alok{ 
    public static void main(String[] args){ 
     int i = 0,j=0; 
     for(i=0;i<10;i++){ 
     System.out.print(""+i+"->"); 
     for(j=(i*3)+1;j<(i*3)+4;j++){ 
      System.out.print(""+j+" "); 
     }System.out.println(); 
     } 
    } 
} 
1

這裏是一個循環的解決方案:

int n = 15; 

for (int i = 0; i < n; i++) { 
    if (i % 3 == 0) { 
     System.out.println(); 
    } 
    System.out.print(i + " "); 
} 
+0

@downvoter,謹慎解釋爲什麼這是downvoted? – rajesh 2013-04-27 04:52:16

0

有很多上面的嵌套循環。這是一個for循環中的可擴展解決方案。

public static void main(String[] args) { 
    int numbersPerLine = 3; 
    int finalNumber = 12; 
    int startingRowNumber = 0; 
    System.out.print(startingRowNumber + " -> "); 
    for(int i = 0; i < finalNumber; i++) { 
     if(i > 0 && (i % numbersPerLine) == 0) { 
      System.out.print("\n" + ((i/numbersPerLine) + startingRowNumber) + " -> "); 
     } else if(i > 0) { 
      System.out.print(",");    
     } 
     System.out.print((i + 1)); 
    } 
}