2016-11-28 42 views
-3

如何在給定開始節點的任何x和y座標的情況下創建板。例如,如果x = 3且y = 2的板謹:開始8個難題

1 2 3 
4 5 x 
6 7 8 

在java或僞代碼的一個例子是極其有用的。

+5

你已經試過了什麼?請顯示你的代碼。 – DimaSan

+4

您必須打印數字1到8,並且您需要打印字符'x'。您還需要在每第三個字符後開始一個新行,直到您到達最後一行。當然,你可以自己做一些*,然後在卡住時尋求幫助。 – Gendarme

回答

0

希望這有助於。如果您有任何問題,請告訴我。

int x_lim = 2; 
int y_lim = 3; 

int count=1; 
for(int x=1;x<3+1;x++) 
{ 
    for(int y=1;y<3+1;y++) 
    { 
     if(x_lim==x && y_lim==y) //skip case (blank tile) 
     { 
      System.out.println("x"+" "); 
     } 
     else //other numbers 
     { 
      System.out.println(count+" "); 
      count++;    
     } 

    } 
} 
+0

我誤解了這個問題,但現在更新了。 –

+0

你是否運行過這段代碼?它不以正確的格式輸出。 – Jason

-1

在發佈代碼之前,我只會推薦一件事。在編程中,你必須開始考慮基於零的索引。另外,如果您發佈的格式在索引編制中沒有任何拼寫錯誤(因爲在打印'x'後理所當然,您必須打印7而不是6,以便3x3電路板在索引9中結束),下面的代碼可能有助於您。

int coord_x = 3; 
    int coord_y = 2; 

    int rows = 3; 
    int columns = 3; 

    int counter = 1; 
    for (int i = 1; i <= rows; i++){ 
     for (int j = 1; j <= columns; j++){ 
      if (i == coord_y && j == coord_x){ 
       System.out.print("x "); 
       continue; 
      } 
      System.out.print(counter + " "); 
      counter++; 
     } 
     System.out.println(); 
    }