为了账号安全,请及时绑定邮箱和手机立即绑定

如何在水平表格中垂直打印

如何在水平表格中垂直打印

白猪掌柜的 2021-09-29 13:29:17
所以我正在制作一个程序,我尝试做的一件事就是打印一个像这样转换后的 ASCII 字符表 DEC HEX OCT  DEC HEX OCT  DEC HEX OCT  DEC HEX OCTA 65 101 25 C 66 111 232  E 12 32  12  G 21 56  12B 12 89 23  D 45 124  23  F 34 123 10  H 89 203 8我已经完成了程序的转换部分我只需要一点帮助让表格以正确的方式打印有人有什么建议吗?我的代码public static void getInputs(String[] args) {    //c1, c2, and c3 are the characters the user enters    //selector is what determends how the method will print    char c1 = args[0].charAt(0);    char c2 = args[1].charAt(0);    char c3 = args[2].charAt(0);    int letter1 = (int) c1;    int letter2 = (int) c2;    String selector = Character.toString(c3);    char[] arr;    int index = 0;    arr = new char[c2 - c1 + 1];    //Yif the selector is the letter h the program will print horizontaly         //Xif the selector is the letter v the program will print vertically     //Yprogram prints horizontaly    //Xprogram prints vertically    //Yselector works    //Xclean up everything    if (letter2 >= letter1){    if (selector.equals("h")) {            //(char)x is the letter            //x is the number            int counter = 0;            for (int x = (int) c1; x <= c2 && counter < 4; ++x) {                System.out.print("     " + "DEC " + "Oct " + "Hex");                ++counter;            }            System.out.println("\n");            for (int x = (int) c1; x <= c2; ++x) {                if (counter % 4 == 0) {                    System.out.println("");                }                String hex = Integer.toHexString(x);                String oct = Integer.toOctalString(x);                arr[index++] = (char) x;                System.out.print("   " + (char) x + "  "                        + x + " " + oct + "  " + hex);                ++counter;            }我知道我的代码有点乱,你可以把东西放在方法中,但现在我只专注于让表格正确打印。我也为我的问题的坏名声道歉。
查看完整描述

3 回答

?
慕容3067478

TA贡献1773条经验 获得超3个赞

我不会展示如何修改您的代码以按水平和垂直顺序打印值序列,而是向您展示一个更通用的实现,用于简单地打印这样的数字,因此答案对其他人也更普遍有用。


我会让你把算法应用到你的代码中。


public static void printHorizontal(int min, int max, int cols) {

    for (int i = min; i <= max; i++) {

        boolean last = (i == max || (i - min + 1) % cols == 0);


        System.out.printf("%3d", i);

        if (last)

            System.out.println();

        else

            System.out.print("  ");

    }

}

public static void printVertical(int min, int max, int cols) {

    int rows = (max - min) / cols + 1;

    for (int row = 0; row < rows; row++) {

        for (int col = 0, i = min + row; col < cols && i <= max; col++, i += rows) {

            boolean last = (col == cols - 1 || i > max - rows);


            System.out.printf("%3d", i);

            if (last)

                System.out.println();

            else

                System.out.print("  ");

        }

    }

}

测试


printHorizontal(5, 17, 5);

printVertical(5, 17, 5);

输出


  5    6    7    8    9

 10   11   12   13   14

 15   16   17

  5    8   11   14   17

  6    9   12   15

  7   10   13   16


查看完整回答
反对 回复 2021-09-29
?
哔哔one

TA贡献1854条经验 获得超8个赞

使用System.out.printf(String, ....)它会变得更容易 - 您可以按如下方式指定项目:


System.out.printf("%c %3d %03o %3x %c %3d %03o %x %c %3d %03o %3x\n", 

    'a', (int)'a', (int)'a', (int)'a', 

    'b', (int)'b', (int)'b', (int)'b', 

    'c', (int)'c', (int)'c', (int)'c');

这让你


a  97 141  61 b  98 142 62 c  99 143  63


查看完整回答
反对 回复 2021-09-29
?
慕斯王

TA贡献1864条经验 获得超2个赞

打印由制表符分隔的值。取自您的代码片段


标题:


            System.out.print("     " + "\t\tDEC " + "\t\tOct " + "\t\tHex");

            ++counter;

        }

价值观:


            System.out.print("   " + (char) x + "\t\t"

                    + x + "\t\t" + oct + "\t\t" + hex);

            ++counter;

        }


查看完整回答
反对 回复 2021-09-29
  • 3 回答
  • 0 关注
  • 171 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信