如何在不更改格式的情況下刪除所有空格以及最好的選擇是什么,如果是的話我會使用 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 }
1 回答

慕容3067478
TA貢獻1773條經驗 獲得超3個贊
你可以這樣做:
System.out.format("%15s%s", "", "Test");
這里有一個長度為 15 的字符串,后面跟著一個未定義的字符串。由于第一個格式化的字符串是空字符串,因此將打印 15 個空格。
添加回答
舉報
0/150
提交
取消