繁体   English   中英

在第 11 行,我该怎么做才能不需要使用那么多空格

[英]In line 11 how do i make it so that i don't need to use so many spaces

如何在不更改格式的情况下删除所有空格,这样做的最佳选择是什么,如果是这样,我会使用 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 个空格。

如何在不更改格式的情况下删除所有空格,最好的选择是什么,如果是这样,我会使用 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.
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM