【发布时间】:2013-07-12 18:10:38
【问题描述】:
表格应该是这样的:
我的表产生了正确的输出。只需要在水平侧对齐值即可。
import java.util.Scanner;
公开课很无聊
{ public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
int rows=0, cols=0;
int a, b;
System.out.print("Enter number of rows from 1-10");
rows = keyboard.nextInt();
System.out.print("Enter number of columns from 1-10");
cols = keyboard.nextInt();
for (int c=1; c<=cols; c++){
System.out.print(" "+c);
}
System.out.println();
System.out.println( "----------------------");
for (int d=1; d<=rows;d++){
System.out.print(""+d);
System.out.println();
}
for ( a=1; a<=rows; a++)
{
for ( b=1; b<=cols; b++)
{
System.out.print(" "+a*b);
}
System.out.println();
}
}
}
【问题讨论】:
-
你真的需要了解循环,尤其是for loop。 this example 也可能会让您感兴趣。
-
同意上面的cmets,还需要看docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
-
是的,我更新了我的代码,但水平方向仍有问题。