2015-12-01 35 views
0

我覺得我寫的代碼是錯誤的,但我不知道爲什麼。對我來說看起來很不專業。這些如何從一個到四個打印四行和四列數字並在達到四個後重新啓動

/* 1234 
    2341 
    3412 
    4123 
*/ 
public class pattern{ 
    public static void main(String args[]){ 
    for(i=1; i<=4; i++) 
    {for(j=1; j<=4; j++) 
     {System.out.print(i); 
     } 
     System.out.println(); 
     while(i>4) 
     { int i= 1; 
      i++; 
      System.out.print(i);} 
     System.out.println(); 
} 
+0

首先,這段代碼是錯誤的。 'string'和'system'(小寫's')都是類。 「我」和「J」從未被宣佈。其次,這個代碼是*可怕*格式。 –

+0

你的while循環是無限循環。你正在初始化循環內的計數器 – Rehman

+0

你的while循環是如何工作的?你的條件是我大於4,但是在循環內你把我初始化爲1,然後加1,所以它變成2.此外,許多其他問題,例如大寫錯誤,初始化循環變量失敗,關閉不足大括號,錯誤的縮進...... – Foleosy

回答

2

很難告訴你要求什麼,但—從你的類以上的評論判斷—你可能尋找的是這樣的:

for (int i = 0; i < 4; i++) { 
    for (int j = i; j < i + 4; j++) { 
     System.out.print((j % 4) + 1); 
    } 
    System.out.println(); 
} 

不言而喻,但我不得不;您應該始終嘗試遵循Java命名/格式標準。

+0

是的,我想這麼謝謝你! – user27691

0

保重:

for(int i=1; i<=4; i++) 
{for(int j=1; j<=4; j++) 

    System.out.println(); 

你應該初始化變量ijSystemsystem 而在main方法的參數應該像String args

public static void main(String args[]){ 

這裏是你的代碼格式不錯:

public static void main(String args[]) { 
    for (int i = 0; i < 4; i++) { 
     for (int j = i; j < i + 4; j++) { 
      System.out.print((j % 4) + 1); 
     } 
     System.out.println(); 

    } 
} 
+0

謝謝親愛的:) – user27691

+0

即使使用固定的格式/縮進,代碼也不會打印正確的值,因爲他的原始代碼工作不正確。 – Foleosy

+0

是的Foleosy你完全正確 – user27691

相關問題