[Java usage] Common usage of \t \n in Java, with an example of Java code with nine-nine multiplication table

Contents of this article

One, \n explain

Two, \t explain


One, \n explain

\n NewLine, new line

 \n It's relatively simple, you can understand it immediately by looking at the example,

System.out.println("aaa\nbbb");

The results are as follows:

Two, \t explain

\t is equivalent to tab, indentation, used in code to indicate formatted output

\t is to complete the length of the current string to an integer multiple of 8, with at least 1 space and at most 8 spaces

How much to fill depends on the length of the string before \t

For example, the current string length is 10, then the length after \t is 16, which means 6 spaces

If the current string length is 12, the length after \t is 16, add 4 spaces

For beginners, the " \t " and spaces in java are always confusing. It doesn't matter what you don't understand above, you can quickly understand it through an example!

public class MultiTable {

    public static void main(String[] args) {
        // 打印九九乘法表
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + i * j + " ");
            }
            System.out.println();
        }
    }
}

This is the printed effect, as shown in the figure below:

Careful, you should soon see that 4*3 and 5*3 cannot be aligned due to the different digits of the number. This is the result of using spaces.

Then we use \t  some people are also called tabs, first look at the code, and then look at the effect:

public class MultiTable {

    public static void main(String[] args) {
        // 打印九九乘法表
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + i * j + "\t");
            }
            System.out.println();
        }
    }
}

The effect of using \t,  also known as a tab character, is as follows:

Now you should be able to understand what the switch says in this article, come on! ! !

 

end!

Guess you like

Origin blog.csdn.net/weixin_44299027/article/details/113715041