猿问

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

所以我正在制作一个程序,我尝试做的一件事就是打印一个像这样转换后的 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;

            }


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


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


白猪掌柜的
浏览 215回答 3
3回答

慕容3067478

我不会展示如何修改您的代码以按水平和垂直顺序打印值序列,而是向您展示一个更通用的实现,用于简单地打印这样的数字,因此答案对其他人也更普遍有用。我会让你把算法应用到你的代码中。public static void printHorizontal(int min, int max, int cols) {&nbsp; &nbsp; for (int i = min; i <= max; i++) {&nbsp; &nbsp; &nbsp; &nbsp; boolean last = (i == max || (i - min + 1) % cols == 0);&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%3d", i);&nbsp; &nbsp; &nbsp; &nbsp; if (last)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("&nbsp; ");&nbsp; &nbsp; }}public static void printVertical(int min, int max, int cols) {&nbsp; &nbsp; int rows = (max - min) / cols + 1;&nbsp; &nbsp; for (int row = 0; row < rows; row++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int col = 0, i = min + row; col < cols && i <= max; col++, i += rows) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean last = (col == cols - 1 || i > max - rows);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%3d", i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (last)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("&nbsp; ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}测试printHorizontal(5, 17, 5);printVertical(5, 17, 5);输出&nbsp; 5&nbsp; &nbsp; 6&nbsp; &nbsp; 7&nbsp; &nbsp; 8&nbsp; &nbsp; 9&nbsp;10&nbsp; &nbsp;11&nbsp; &nbsp;12&nbsp; &nbsp;13&nbsp; &nbsp;14&nbsp;15&nbsp; &nbsp;16&nbsp; &nbsp;17&nbsp; 5&nbsp; &nbsp; 8&nbsp; &nbsp;11&nbsp; &nbsp;14&nbsp; &nbsp;17&nbsp; 6&nbsp; &nbsp; 9&nbsp; &nbsp;12&nbsp; &nbsp;15&nbsp; 7&nbsp; &nbsp;10&nbsp; &nbsp;13&nbsp; &nbsp;16

哔哔one

使用System.out.printf(String, ....)它会变得更容易 - 您可以按如下方式指定项目:System.out.printf("%c %3d %03o %3x %c %3d %03o %x %c %3d %03o %3x\n",&nbsp;&nbsp; &nbsp; 'a', (int)'a', (int)'a', (int)'a',&nbsp;&nbsp; &nbsp; 'b', (int)'b', (int)'b', (int)'b',&nbsp;&nbsp; &nbsp; 'c', (int)'c', (int)'c', (int)'c');这让你a&nbsp; 97 141&nbsp; 61 b&nbsp; 98 142 62 c&nbsp; 99 143&nbsp; 63

慕斯王

打印由制表符分隔的值。取自您的代码片段标题:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("&nbsp; &nbsp; &nbsp;" + "\t\tDEC " + "\t\tOct " + "\t\tHex");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++counter;&nbsp; &nbsp; &nbsp; &nbsp; }价值观:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("&nbsp; &nbsp;" + (char) x + "\t\t"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + x + "\t\t" + oct + "\t\t" + hex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++counter;&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答