java - 如何在水平表格中垂直打印
问题描述
所以我正在制作一个程序,我想做的一件事就是打印一张这样的转换后的 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;
}
价值观:
System.out.print(" " + (char) x + "\t\t"
+ x + "\t\t" + oct + "\t\t" + hex);
++counter;
}
推荐阅读
- certificate - 安装屏蔽数字证书
- extjs - 选择选取器未在移动模式下启动边缘选取器
- java - 系统中的通知声音已静音
- sign - 文件自签名后已被修改或损坏!(数据)
- python - (导入错误)由于尝试安装 gensim.models,因此无法使用 pip 安装软件包
- c++ - 如何将 Void* 转换为 size_t?
- node.js - NodeJS如何获取返回400 Bad Request的请求正文
- google-fit - 用户在特定时间的速度数据
- javascript - 如何使用javascript查找与字段匹配的项目?
- javascript - Vanilla Javascript 多个相同的选择,当您选择 1 个选项时,所有其他选择更改为相同的选项