2011-12-06 98 views
0

這不是家庭作業。我是一名初學者(新手)的java程序員,試圖在ivor horton開始的java書的末尾閱讀並完成練習。如何在矩形輸出中顯示二維數組?

編寫一個程序來創建一個包含從1 X 1到12 X 12的乘法表的矩形數組。將該表輸出爲13列,並將數值右對齊列。 (輸出的第一行是列標題,第一列沒有標題,然後是其餘列的數字1-12。每個後續行的第一項是行標題,範圍從1-12。

注意:我只學過數組&字符串,循環&邏輯,數據類型,變量和計算我沒有了解類和它們的方法等......所以沒有花哨的東西請。謝謝!

public class Chapter4Exercise2 { 

    public static void main(String[] args) 
    { 

    int[][] table = new int[12][12]; 


    for(int i=0; i <= table.length-1; i++) 
    { 
     for (int j=0; j <= table[0].length-1; j++) 
     { 
     table[i][j] = (i + 1) * (j + 1); 
     if (table[i][j] < 10) 
     System.out.print(" " + table[i][j] + " ");   
     else 
     if (table[i][j] > 10 && table[i][j] < 100)   
     System.out.print(" " + table[i][j] + " "); 
     else 
     System.out.print(table[i][j] + " ");  
     } 
     System.out.println(" "); 
    } 

    } 
} 

回答

2

只要數未達1000,試試這個:

正如@ Mr1158pm說:

public class Chapter4Exercise2 { 
    public static void main(String[] args) { 

    int tableSize = 10; 

    int[][] table = new int[tableSize][tableSize]; 

    for(int i=0; i < table.length; i++) { 
    for (int j=0; j < table[i].length; j++) { 
     table[i][j] = (i+1)*(j+1); 

     if(table[i][j] < 10) //Where i*j < 10 
     System.out.print(" "+(table[i][j])+" "); 
     else if(table[i][j] < 100) //Where i*j < 100 
     System.out.print(" "+(table[i][j])+" "); 
     else //Where i*j < 1000 
     System.out.print(" "+(table[i][j])+" "); 
    } 
    System.out.println(""); 
} 
+0

+1簡單,並統計它的限制。 –

+0

嗨,對不起,我想我應該明確說明我需要使用一個數組來做到這一點,但是,我假設上面的語句「編寫一個程序來創建一個矩形陣列」爲我處理。無論如何,是的,我需要用雙暗陣列來做到這一點 - 謝謝 – IElite

+0

編輯使用2-D'int'數組。 – Jon

0

我不認爲你要申報陣列打印出此表。 在剛剛使用兩個嵌套的for循環和做Calcs(計算)循環。 以下是您可以使用的工作方法。只需要修正列對齊,在這裏和那裏添加空間。 FYI row < 10?「」+ row:row是inline if語句的表單。如果你在Google之前沒有看過它。這非常有用。

public static void main(String[] args) { 

    for(int row=0; row<13; row++) 
    { 
     for(int col=0; col<13; col++) 
     { 
      if(row==0) //ffirst row 
      { 
       if(col==0) 
        System.out.print(" "); 
       else 
        System.out.print(col<10?" "+col:" "+col); 
      } 
      else 
      { 
       if(col==0) 
        System.out.print(row<10?" "+row:row); 
       else 
        System.out.print(row*col<10?" "+(row*col):(row*col<100? " "+(row*col):" "+(row*col))); 
      } 
     } 
     System.out.println(); 
    } 
} 
+0

感謝,但我對章「數組和字符串「,因此,我應該嘗試瞭解使用數組 – IElite

0
import java.util.Scanner; 
public class Back { 
public static void main(String[] args) { 
    Scanner a1 =new Scanner(System.in); 
    int row,col; 
    String ch; 
    System.out.println("Enter width of screen:"); 
    row = a1.nextInt(); 
    System.out.println("Enter height of screen:"); 
    col = a1.nextInt(); 
    System.out.println("Enter background character:"); 
    ch =a1.next(); 
    String twoD[][] = new String[row][col]; 
    int i,j; 

    for(i=0;i<row;i++) 
     for(j=0;j<col;j++){ 
      twoD[i][j] = ch; 

     } 
    for(i=0;i<row;i++){ 
     for(j=0;j<col;j++) 
    System.out.print(twoD[i][j]+" "); 
    System.out.println(); 
    }