【问题标题】:Formatting tables in Java在 Java 中格式化表格
【发布时间】:2014-02-28 03:53:30
【问题描述】:

这是本次作业的主要步骤。创建嵌套循环以打印出 如果输入有效,则为表格格式的数字。第一个输入 i 定义数字
行数,第二个输入 j 定义列数。在循环内部, 编写代码列出从 1 到 i*j 的正数。使用 System.out.printf() 具有“%4d”和“%4s”格式的函数 整数和字符串。

我需要打印出来

设置表的大小和数据类型(输入'Q'或'q'退出): 3 4 个“数字”

  • | 1 2 3 4

----------------

1 | 1 2 3 4

2 | 5 6 7 8

3 | 9 10 11 12

在我设置了列数和行数后,我只能得到内部表,而不是外部数字表或星号。 为了让它正确显示,我不得不改变它,但它应该排列整齐

    Scanner scan = new Scanner(System.in);
    String stringIn;

    do
    {
        System.out.print("Set the size of table and data-type(Type Q or q to quit)");
        stringIn = scan.nextLine();
        int space = stringIn.indexOf(" ");
        int spaceTwo = stringIn.indexOf(" ", space+1);

        if(stringIn.length()>4)
        {

            String firstNum = stringIn.substring(0,space);
            String secondNum = stringIn.substring(space+1,spaceTwo);
            String dataType = stringIn.substring(spaceTwo+1);
            int firstInt = Integer.parseInt(firstNum);
            int secondInt = Integer.parseInt(secondNum);

            if (!stringIn.equals("Q")&&!stringIn.equals("q")&&firstInt>=0&&secondInt>=0)
            {
                System.out.println(stringIn.substring(0,space) + " " + stringIn.substring(space+1,spaceTwo) + " " + stringIn.substring(spaceTwo+1));
            }
            else if(firstInt<0||secondInt<0)
            {
                System.out.println("Try again. The input was invalid");
            }
            for(int i = 1; i <firstInt+1; i++)
            {
                for (int j = 1; j < secondInt+1; j++)
                {
                    System.out.printf("%4d", i*j);
                    System.out.printf("%4s", i*j);
                }
                System.out.println();
            }
        }

    }
    while(!stringIn.equals("Q")&&!stringIn.equals("q"));

这是我的第一个 Java 类,所以我的代码非常混乱。

【问题讨论】:

    标签: java format tabular


    【解决方案1】:

    您已经很接近了,只是与嵌套循环中的逻辑有些不同。这是我修改的:

    1) 将列标签移到内循环之外

    2) 为单元格值创建了一个计数器

    3) 使用计数器打印单元格值然后递增它

    代码:

    String headerRow= " * |"; 
    String spacer = "-----";
    for(int i=1; i<secondInt + 1; i++){headerRow+="   "+i; spacer+="----";}
    System.out.println(headerRow);
    System.out.println(spacer);
    int counter = 1;
    for (int i = 1; i < firstInt + 1; i++) {
        System.out.printf("%4s", i + " |");
        for (int j = 1; j < secondInt + 1; j++) {
            System.out.printf("%4d", counter);
            counter++;
        }
        System.out.println();
    }
    

    该代码输出如下:

    > 4 5 numbers  
    * |   1   2   3   4   5
    > -------------------------  
    1 |   1   2   3   4   5  
    2 |   6   7   8   9  10  
    3 |  11  12  13  14  15  
    4 |  16  17  18  19  20
    

    【讨论】:

    • 谢谢,这是一个很大的帮助,但我将如何让第一行显示“* | 1 2 3 4”(行号)“------------- -" 下面有一个分隔符
    • 我用循环上方的几个System.out.println 语句更新了答案,应该让你得到你想要的!
    • 谢谢,但是如果我选择使它超过 4 列,我想要得到的是随着表格增长的东西,我无法弄清楚如何获得该输出,第一行需要是一个列计数器,所以如果有 3 个它将上升到 4、5 到 5 等等。
    • 啊,我的错,在静态思考,再次更新它,现在应该是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2018-05-22
    • 2018-06-04
    相关资源
    最近更新 更多