如何在java中的列中显示乘法表。没有使用额外的java函数,只是循环和条件

时间:2013-02-24 23:51:55

标签: java loops conditional

我想在三列和三行中显示值。这是我的代码:

public class MultiplicationTable {

    public static void main(String[] args) {

        for(int i=2;i<=10;i++){
            System.out.println();
            System.out.println(" The table " + i);
          for(int j=2;j<=10;j++) {
              System.out.println(" " +i + " x " + j + " = " + i*j + "\t");
          }
       }
    }
}

3 个答案:

答案 0 :(得分:0)

(更正)我的愚蠢;只是普通的表,但在3列。

而不是“x”你可以写“\ u00d7”或“×”这是真正的数学运算符×。

public static void main(String[] args) {
    final String TAB = "    ";
    for(int y = 0; y < 3; y++) {
        for(int j = 2; j <= 10; j++) {
            for (int x = 0; x < 3; x++) {
                int i = 2 + y*3 + x;
                System.out.printf("The table %2d  " + TAB, i);
                System.out.printf("%2d x %2d = %3d" + TAB, i, j, i*j);
            }
            System.out.println();
        }
        System.out.println();
    }
}

答案 1 :(得分:0)

for(int i=2;i<=10;i++){
    System.out.println();
    System.out.println(" The table " + i);
    for(int j=2;j<=10;j++) {
        System.out.println( i + " x " + j + " = " + i*j );
    }
}

答案 2 :(得分:0)

String temp     = "";
String lbl      = "TABLE #";

int num         = 10,
    rowLength   = 10,
    colLength   = 3;

int tableNumber = 1, 
    counter     = colLength,
    index       = 1;


for ( int colLabel = index; colLabel <=num; colLabel++, tableNumber++ ){                        
    System.out.printf("%11s %-10s",lbl + tableNumber, "");

    if ( colLabel == counter || colLabel == num ){
        System.out.println();
        for ( int row = 1; row <=rowLength; row++){
            for ( int column = index; column <=colLabel; column++){
                temp    = String.format("%4d x %-3d= ", column, row );
                temp   += String.format("%-10d",(column * row));
                System.out.printf("%20s",temp);
            }   
            System.out.println();
        //  temp = "";
        }

        System.out.println("\n");
        index    = (tableNumber + 1);                               
        counter += colLength;   
    }                               
}