A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 年轻的棒棒 中级黑马   /  2013-4-18 09:17  /  2495 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 年轻的棒棒 于 2013-5-21 10:17 编辑
  1. public class Test6{
  2.         public static void main(String[] args){
  3.                 multiPlication();
  4.         }
  5.         public static void multiPlication(){
  6.                 for(int i=1;i<=9;i++){
  7.                         for(int j=1;j<=i;j++) {
  8.                                 System.out.print(i + "*" + j + "=" + i * j + " ");                                
  9.                         }
  10.                         System.out.println();
  11.                 }
  12.                
  13.         }
  14. }
复制代码
打印九九乘法表,代码很简单,但是结果不是很完美,
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
4*3和5*3不能对齐啊,求解决啊,这是控制台程序,不用表格的话怎么控制完全对齐啊!

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

9 个回复

倒序浏览
建议还是使用制表符:“\t”,否则打印的乘法表终究都不完美
  1. class  For99
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 print99();
  6.         }
  7.         public static void print99()
  8.         {
  9.                 for(int x=1;x<=9;x++)
  10.                 {
  11.                         for(int y=1; y<=x;y++)
  12.                         {
  13.                                 System.out.print(y+"*"+x+"="+y*x+"\t");//加入\t则可自动对齐,形成制表符
  14.                         }
  15.                         System.out.println();
  16.                 }
  17.         }
  18. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
因为LZ你的乘法结果从一位数变为两位数,导致后面的格式不能对其,一般打印的时候都会在后面加上制表符使格式对齐!这个问题你可以参考JAVA基础day04-4的讲解!
public class Demo11
{
        public static void main(String[] args)
                {
                multiPlication();
        }
        public static void multiPlication()
                {
                for(int i=1;i<=9;i++)
                                {
                        for(int j=1;j<=i;j++)
                                                {
                                System.out.print(i + "*" + j + "=" + i * j + "/t");在结尾处添加制表符,可以使排版对齐!                                
                        }
                        System.out.println();
                }

        }
}

评分

参与人数 1黑马币 +2 收起 理由
Miss小强 + 2

查看全部评分

回复 使用道具 举报
在输出语句中加入\t 制表符
即:System.out.print(y+"*"+x+"="+y*x+"\t");
就OK了
回复 使用道具 举报
System.out.print(i + "*" + j + "=" + i * j + "/t"); 加个/t  制表符就OK 了
比老师视频里面有讲到
回复 使用道具 举报
wlt 发表于 2013-4-18 09:55
System.out.print(i + "*" + j + "=" + i * j + "/t"); 加个/t  制表符就OK 了
比老师视频里面有讲到 ...

加上制表符,还是用到了表格啊!我说了不用表格的话!
回复 使用道具 举报
wlt 发表于 2013-4-18 09:55
System.out.print(i + "*" + j + "=" + i * j + "/t"); 加个/t  制表符就OK 了
比老师视频里面有讲到 ...

加上制表符,还是用到了表格啊!我说了不用表格的话!
回复 使用道具 举报
  1. public class Test6 {

  2.         /**
  3.          * 6、 用控制台程序输出九九乘法表;输出结果按下图所示:
  4.       1*1=1
  5.       1*2=2   2*2=4
  6.       1*3=3   2*3=6   3*3=9
  7.        .........
  8.          */
  9.         public static void main(String[] args) {
  10.                 for(int n=1;n<=9;n++){
  11.                         for(int m=1;m<=n;m++){
  12.                                 System.out.print(m+"*"+n+"="+m*n+"\t");
  13.                         }
  14.                         System.out.println();
  15.                 }
  16.         }

  17. }
复制代码
LZ可以参考下

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
  1. /*
  2.                
  3.                 九九乘法表
  4.                 1*1=1
  5.                 1*2=2 2*2=4
  6.                 1*3=3 2*3=6 3*3=9

  7.                
  8.                 */

  9.                 for (int x=1; x<=9 ; x++)
  10.                 {
  11.                         for (int y=1; y<=x; y++)
  12.                         {
  13.                                 System.out.print(y+"*"+x+"="+y*x+"\t");
  14.                         }
  15.                         System.out.println();
  16.                 }

  17.         }
  18. }
复制代码
回复 使用道具 举报

学的很到位啊,思维开阔了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马