gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:47:54 25 4
gpt4 key购买 nike

所以我正在编写一个程序,我想做的事情之一就是打印一张转换后的 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.");
}

}

我知道我的代码有点困惑,您可以将内容放入方法中,但现在我只专注于让表格正确打印。

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

最佳答案

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

标题:

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

值(value)观:

            System.out.print("   " + (char) x + "\t\t"
+ x + "\t\t" + oct + "\t\t" + hex);
++counter;
}

关于java - 如何在水平表格中垂直打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52411780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com