[Java Basics Day 10] Commonly used escape characters in Java

1, \t: a tab stop, realize the alignment function

\t:一个制表位,实现对齐功能
public class ChangChar{
    
    
	public static void main(String[] args) {
    
    
		//  \t:一个制表位,实现对齐的功能
		System.out.println("三国演义\t作者\t罗贯中");

insert image description here

2. \n: line break

\n: newline character

System.out.println("Tom\njack\nDanny");

insert image description here

3. \: One escape character

\\ : escape character a \ \\

Before talking about \\ escape characters, please look at a picture.
insert image description here
If you want to output a \, then add a \ after \ (this \ represents the output structure), so that when the computer executes the program, it will Think that the first \ is an escape character, and the following \ is the output statement you need. How is it possible? I do not believe.

System.out.println("cmd\\windows\\system2\\cmd.exe");

insert image description here
The real output is coming, ah this (slap in the face)!

What should I do if I want to output \\? I mean, is there a possibility, it’s impossible (hehehe), it’s impossible, it
can’t be realized, the method of realization is very simple: \\\\ four backslashes, why I suddenly can’t understand here ( It's crashing in a daze...)
If you don't understand it, it doesn't matter, let's run it first.

System.out.println("cmd\\\\windows\\\\system2\\\\cmd.exe");

insert image description here

oh! I seem to understand something (suddenly, I suddenly realized it!) Just now you said before: If you want to output a \, then add a \ after \ (this \ represents the output structure), then the computer is executing the program , it will think that the first \ is an escape character, and the following \ is the output statement you need. Following your train of thought, let’s infer other cases from one instance. If we want to output two \\, then put it after \ Add a \\ (this \\ represents the output structure), then when the computer executes the program, it will think that the first \\ is an escape character, and the following \\ is the output statement you need, we press this Ideas, and so on, we can output five \ or even six \. It turned out to be really that simple!

4. ":one"

" : one"

Requirements: In someone's statement, emphasize the following content.
So easy, let me do it! Look at mine (hehehe, it's time for me to show my talents)
System.out.println("Zhang San said: "Study criminal law hard, you will have a rich future"");
insert image description here
Oops! What is this, ah this, I don’t understand, but I was quite shocked, in my impression, this should not happen in operation, ah, this, I was dumbfounded! Why is it reporting an error? Don't complain, I'll run my test.

System.out.println("张三说:\"要好好学习刑法,有钱途\"");

insert image description here
Good guy, why didn't you report an error, ah this! Please explain the principle, I don’t understand where I have a problem, please give me advice (ask with humility)

how to say? According to your way of writing, when the computer executes this program, it executes to "Zhang San said: here the execution has stopped (the computer thinks Zhang San said: it is a whole), and it will not recognize the subsequent programs (change One way of saying: the computer can’t understand whether to output the sentence that Zhang San said or the next sentence, according to its thinking, it thinks that what you want to output is the whole that Zhang San said)

The reason why the program I wrote can run is because I added a backslash after Zhang San said: and Qianqiantu respectively. This backslash is to tell the computer to execute the program until Zhang When San said, escape what Zhang San said (tell the computer: this is a complete sentence, and emphasize the content behind what he said.)

Can you still play like this? computer: 666, my computer: zero to zero

5. ':a'

\':one'

System.out.println("张三说:\'要好好学习刑法,有钱途\'");

This is almost the same as above, so I won’t go into details.

6. \r: a carriage return System.out.println("Learning criminal law\r has a rich way")

\r: means carriage return

 System.out.println("刑法教育\r可刑");

I am not in a hurry to run this program. Before the program runs, you can guess what will be output?
Then I will guess, is there a prize for guessing correctly? Yes, I have! (Hey hey hey) If there is a prize, that's fine!

According to my thinking: it will output criminal law education and then output in a new line. It is
feasible (I see the \r in the comment means press enter, and the next line will be executed after pressing enter).
I have a different idea from yours. How do you say it? Talk about your thinking, I think it will output criminal education, impossible, absolutely impossible, am I still blind? I can't read the word criminal law clearly (hhh) If you don't believe me, let's run it and see the result.
Let's verify who guessed right? Come and come (Stand up and speak, it's tough!)
insert image description here
Ah, here! This is not impossible, it...no...should...press enter to output the next sentence? How can criminal education be exported?

What you said is not entirely wrong, at least half of what you said is right, how should I put it?

After the program executes to the criminal law education (assuming there is a virtual cursor behind), it sees a carriage return \r behind it, and the cursor behind the criminal law education will directly push to the front of the execution program. Replaced, the law at this time has also been replaced by the following sentence, so the output result is: Ke Xing education, at this time my brain is buzzing... It’s okay, anyway, it’s more than 00 o’clock now, simple Take a shower, lie on the bed, and understand why? Should not be difficult to understand.

What if I want to output the content behind \r?
In fact, it is not difficult, just add a \n after \r. As for why it has been mentioned before, it can be easily understood.

 System.out.println("刑法教育\r\n可刑");

insert image description here

7. Application examples (Exercise)

Requirement: Please use an output statement to achieve the effect of outputting the following graphics (limited to 3 minutes).
insert image description here
How did everyone make it? You can type out your own execution statement in the comment area and discuss with each other.

At this point, the escape characters commonly used in Java are introduced here. The city fell into a gentle sleepiness, and the moon began to wake up its eyes on the branches. Good night!

Guess you like

Origin blog.csdn.net/qq_62259825/article/details/128414129