首页 > 解决方案 > 如何在水平表格中垂直打印

问题描述

所以我正在制作一个程序,我想做的一件事就是打印一张这样的转换后的 ASCII 字符表

 DEC HEX OCT  DEC HEX OCT  DEC HEX OCT  DEC HEX OCT
A 65 101 25 C 66 111 232  E 12 32  12  G 21 56  12
B 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;
            }
            pause();
        } else if (selector.equals("v")) {


         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 + "\n");
                ++counter;
            }
            pause();




        } else {
            System.out.println("Error: Third input parameter must be h or v.");
            System.out.println("Program now terminating.");
            //?insert pause and end the program here
        }
    }else{
    System.out.println("Error: First input parameter must precede "
    + "the second in the ASCII sequence.");
    }

}

我知道我的代码有点乱,你可以把东西放在方法中,但现在我只专注于让表格正确打印。

我也为我的问题的坏名声道歉。

标签: javaarrays

解决方案


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

标题:

            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;
        }

推荐阅读