java - Java:格式化包含整数和数组的输出
问题描述
各位晚上好,
我正在为我的计算机科学期末考试尝试一些练习题,但在让我的输出与预期的完全匹配时遇到了一些麻烦。我不确定这是否是我的编译器的限制,或者我只是没有完全理解错误。
基本上,我得到了一个二维数组,我必须在每个向上计数的索引前面输出一个数字。我有,我的问题是输出格式不正确。
这应该是这样的:
我做了很多谷歌,我就是想不通。考虑到它需要的样子,我可以看出数字和字符串是右对齐的,因此对此进行了补偿,我还在每个字符串之后添加了一个制表符,以确保字符串和下一个整数之间的分隔。但是,由于某种原因,事情并没有完全正确对齐。
这是我的格式化输出代码:
int count = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
System.out.printf("%2d", count); //Prints each count, right aligned with a width of two.
System.out.printf("%15s", seasons[i][j]); //Prints each season right aligned with a width of 15
// (largest String is 9 characters).
System.out.printf("\t"); //Adds a tab after the previous season to align the next number.
count++; //Increases count
}
System.out.println();
}
解决方案
推荐阅读
- mysql - 通过将单个消息发布到 kafka 来更新 mysql 中的多个字段
- java - http服务器是否在verticle上运行并拥有自己的事件循环?
- python - 我想反转以下代码的输出格式(行和行号都应该是相反的顺序)
- git - 推送新创建的分支时,远程主机错误消息关闭与 gitlab 的连接
- java - 带有配置文件的 gradle 构建文件名
- php - 如何使用 query() 函数从多个表中实现 Laravel 查询?
- flutter - 在颤动中保持所有变量和函数私有是一种好习惯吗?
- ruby-on-rails - 错误发生意外错误:“https://registry.yarnpkg.com/@rails%2fujs: getaddrinfo ENOTFOUND registry.yarnpkg.com”
- azure - Azure 发布管道 - Azure 密钥保管库任务 VS 变量组
- html - 如何将json值转换为html表