如何打印输出九九乘法表,格式要求如下

Calvin_Lee 2008-07-15 09:38:49
需要打印九九乘法表 格式如下:
* 1 2 3 4 5 6 7 8 9
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64
9 9 18 27 36 45 54 63 72 81
...全文
831 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
diaobiyong 2008-07-15
  • 打赏
  • 举报
回复
public class MultiplyTable {

/**
* @param args
*/
public static void main(String[] args) {
for(int i = 1; i < 10;i++)
for(int j = 1 ;j <= i ;j++)
{
if(i==j)
{
System.out.print(i*j+" ");
System.out.println();
}
else
System.out.print(i*j+" ");
}
}

}
Calvin_Lee 2008-07-15
  • 打赏
  • 举报
回复
谢谢!!
楼上的大家的帮忙
捏造的信仰 2008-07-15
  • 打赏
  • 举报
回复
public class MultiplyTable {

public static void main(String[] args) {
printFirstLine();
printOthers();
}

private static void printOthers() {
for (int i = 1; i < 10; i++) {
printLine(i);
}
}

private static void printLine(int n) {
String str = n + " ";
for (int i = 1; i <= n; i++) {
str += (i * n) + ((i * n) >= 10? " ": " ");
}
System.out.println(str);
}

private static void printFirstLine() {
System.out.print("* ");
for (int i = 1; i < 10; i++) {
System.out.print(i + " ");
}
System.out.println();
}
}
BatiTan 2008-07-15
  • 打赏
  • 举报
回复
Eclipse下运行如下:

* 1 2 3 4 5 6 7 8 9
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64
9 9 18 27 36 45 54 63 72 81
BatiTan 2008-07-15
  • 打赏
  • 举报
回复
如下实现,呵呵
没有写成单独的方法,不过也还可以的~

public class MultTable {

/**
* @param args
*/
public static void main(String[] args) {
int[] row= new int[]{1,2,3,4,5,6,7,8,9};
for(int i= 0; i<= row.length; i++){
for(int j= 0; j<= row.length; j++){
if(i== 0&& j== 0){
System.out.print("* ");
}else if(i== 0&& j!= 0){
System.out.print(j+ " ");
}else if(i!= 0&& j==0){
System.out.print(i+ " ");
}else if(i>= j){
System.out.print((j* i)+ " ");
}
}
System.out.println();
}
System.out.println();
}
}
zhizhuo89 2008-07-15
  • 打赏
  • 举报
回复
public class MultTable {

public static void main(String[] args) {
int[] row= new int[]{1,2,3,4,5,6,7,8,9};
for(int i= 0; i<= row.length; i++){
for(int j= 0; j<= row.length; j++){
if(i== 0&& j== 0){
System.out.printf(" *");
}else if(i== 0&& j!= 0){
System.out.printf("%4d",j);
}else if(i!= 0&& j==0){
System.out.printf("%4d",i);
}else if(i>= j){
System.out.printf("%4d",(j* i));
}
}

在jdk1.5输出为:
C:\>javac MultTable.java

C:\>java MultTable
* 1 2 3 4 5 6 7 8 9
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64
9 9 18 27 36 45 54 63 72 81
在2楼基础上加一个格式化输出。
kinghukyoun 2008-07-15
  • 打赏
  • 举报
回复
一开始想到的和3楼差不多
4楼做得更好,想到了数字的宽度

6楼好像没有打印出第一行吧。

我也和搂主一样是新手,互相勉励吧。
zhizhuo89 2008-07-15
  • 打赏
  • 举报
回复
public class MultTable {
public static void main(String[] args) {
int[] row= new int[]{1,2,3,4,5,6,7,8,9};
for(int i= 0; i<= row.length; i++){
for(int j= 0; j<= row.length; j++){
if(i== 0&& j== 0){
System.out.printf(" *");
}else if(i== 0&& j!= 0){
System.out.printf("%4d",j);
}else if(i!= 0&& j==0){
System.out.printf("%4d",i);
}else if(i>= j){
System.out.printf("%4d",(j* i));
}
}

在jdk输出为:
C:\>java MultTable
* 1 2 3 4 5 6 7 8 9
1 1
2 2 4
3 3 6 9
4 4 8 12 16
5 5 10 15 20 25
6 6 12 18 24 30 36
7 7 14 21 28 35 42 49
8 8 16 24 32 40 48 56 64
9 9 18 27 36 45 54 63 72 81

在2楼的基础上增加格式化输出。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧