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

时间:2018-09-19 18:04:17

标签: java arrays

因此我正在编写程序,而我试图做的一件事就是打印一张这样的转换后的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.");
    }

}

我知道我的代码有些混乱,您可以在方法中放入内容,但现在我只是专注于使表格正确打印。

我也为我的问题的名字表示歉意。

3 个答案:

答案 0 :(得分:1)

打印用制表符分隔的值。摘自您的代码段

标题:

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

答案 1 :(得分:0)

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

由我决定将算法应用于代码。

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

答案 2 :(得分:0)

使用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