如何在矩形输出中显示二维数组?

时间:2011-12-06 01:55:49

标签: java multidimensional-array

这不是作业。我是初学者(新手)java程序员,试图在ivor horton开始java书的末尾阅读并完成练习。

编写程序以创建包含1 X 1到12 X的乘法表的矩形数组12.将表格输出为13列,数值右对齐列。 (输出的第一行是列标题,第一列没有标题,然后是剩余列的数字1-12。每个后续行中的第一项是行标题,范围为1-12。

注意:我只学习了Arrays& amp;字符串,循环和&逻辑,数据类型,变量和计算。我还没有学过课程和他们的方法等......所以请不要花哨的东西。谢谢!

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(" ");
     }

    }
}

4 个答案:

答案 0 :(得分: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("");
}

答案 1 :(得分:0)

我认为你不必声明一个数组数据结构来打印出这个表。 只需使用两个嵌套的for循环并在循环中进行计算。  这是您可以使用的工作方法。只需要修复列对齐,在这里和那里添加空间。 FYI行&lt; 10?“”+ row:row是内联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();
    }
}   

答案 2 :(得分: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();
    }

答案 3 :(得分:0)

int width = 10;
int height = 10;

int[][] table = new int[width][height];

for(int i = 0; i < width; i++) {
    for(int j = 0; j < height; j++) {
            System.out.print(" " + table[i][j] + " ");
    }
    System.out.println("");
}