java - 在第 11 行中,我如何做到这样我就不需要使用这么多的空格
问题描述
如何在不更改格式的情况下删除所有空格,最好的选择是什么,如果是这样,我会使用 System.out.printf(); 或其他一些指令。请用 java 而不是 C++ 回复。我真的很感谢任何帮助我的人,并且很乐意感谢他们帮助我解决我自己的问题。
public class WorldPopulation{
//main string
public static void main(String[] args) {
//prints title
System.out.println(" This is a estimate of world population growth within 300 year time span. ");
//prints number value
System.out.println(" Numbers are in millions");
//number of rows and columns
final int ROWS = 6;
final int COLUMNS = 7;
//population numbers array
int [] [] populations =
{
{106, 107, 111, 133, 221, 767, 1766},
{502, 635, 809, 947, 1402, 3634, 5268},
{2, 2, 2, 6, 13, 30, 46},
{163, 203, 276, 408, 547, 729, 628},
{2, 7, 26, 82, 172, 307, 392},
{16, 24, 3, 74, 167, 511, 809}
};
//continents array
String[] continents =
{
"Africa",
"Asia",
"Australia",
"Europe",
"North America",
"South America"
};
//prints the years used in the estimation
System.out.println(" Year 1750 1800 1850 1900 1950 2000 2050");
//print population data
for (int i = 0; i < ROWS; i++)
{
//print i'th row
System.out.printf("%20s", continents[i]);
//prints the columns and the rows
for (int j = 0; j < COLUMNS; j++) {
System.out.printf("%5d", populations [i] [j]);
}
System.out.println(); //start new line at end of row
}
//print column totals
System.out.printf("%20s","World total");
//prints the total estimated population in the years used
for (int j = 0; j < COLUMNS; j++)
{
int total = 0;
for (int i = 0; i < ROWS; i++) {
total = total + populations[i][j];
}
//format of the printed totals
System.out.printf("%5d", total);
}
//starts new line after each row
System.out.println();
}
//end of class and program.
}
解决方案
你可以这样做:
System.out.format("%15s%s", "", "Test");
那里有一个长度为 15 的字符串,后跟一个未定义的字符串。因为要格式化的第一个字符串是一个空字符串,所以您将打印 15 个空格。
推荐阅读
- python - 不明白 RuntimeError 的原因:在当前进程完成之前尝试启动一个新进程
- azure - Azure NSG 阻止到 docker 容器的流量
- javascript - 我应该在反应打字稿中将什么类型传递给我的 imageIcon 道具?
- java - JBOSS 7 上 servlet 和 JSP 之间的通信
- c# - 输入按钮的值应在单击事件后使用 jquery 更改
- cmake - CMake无法从导入项目的依赖项中找到标头
- c# - Linq:从列表中的多个类成员中选择
- git - 如何在本地机器上返回到前一个头部
- android - 是否可以在使用 MediaRecorder 录制视频/音频的同时使用双簧管进行音频 I/O?
- nginx - 阻止具有特定字符串/单词 nginx 的请求