lesson 2

Hi everyone, welcome back everyone.

 

In the last class, we learned how to write a simple Java program, hello world. From this we know:

1. Java programs are composed of class files, and all programs must be written in the class

The class is defined as follows:

public class class name {

}

2. The entry of a Java program is a function named main, which is written in a fixed way. Any Java program must have a main function before it can run.

The main function is written as follows:

public static void main(String[] args){

}

3, Java programs can output to the screen by calling the System.out.println() method

 

In the last class, we also left an assignment, that is, after hello world, print out hello your name, I wonder if everyone has done it?

 

In fact, just after the first print statement, press Enter, enter sysout, select the list that pops up, and a new System.out.println statement will come out.

In parentheses, type "hello your name", such as "hello jason", and you're done.

Try running it, remember how to run a Java program? By the way, right-click the class file, select Run as, and select Java Application.

 

Look, the results are out, and after Java says hello to the world, it starts to say hello to us.

From this exercise, we found that Java statements are executed sequentially, that is, the statements in the front are executed first, and the ones in the back are executed later.

And we found that between Java statements and statements, there needs to be a; sign to separate.

 

 

Well, we now have a Java program that can say hello to the world and jason. When I showed it to my little friend Jack, Jack said, this program is too stupid, my name is Jack, how did he treat me

 

说Hello Jason。

Yes, wouldn't it be cool if our program could know who the person in front of us is and then greet him. Can Java do this?

 

Sure, let's see how it should be done.

First of all, we need to know who the person in front of us is, which requires the user to tell the Java program what his name is. But in what way? We can easily think of it through the keyboard.

So in Java, how to get the user's input through the keyboard? You can use the following statement:

(new Scanner(System.in)).next();

Now we don't need to figure out this statement, we just need to know that this statement can get the user's keyboard input.

 

Next, we need to save the name entered by the user for use in the print statement. What should we do?

At this time, we need to define a variable. In computer programming, we often use variables to store data.

So how to declare a variable in Java? It is through the following statement:

data type name variable name

For example, String name, the declaration statement declares a variable whose name is name and whose data type is string. A string in Java represents a sequence of characters.

 

We have variables, so how do we store the obtained data in variables? With the assignment statement =, the value on the right side of the equal sign is assigned to the variable on the left side of the equal sign

variable name = data

If name = "jason", the string "jason" will be assigned to the name variable.

Let's pass the data we just got from the keyboard to the variable name

name = (new Scanner(System.in)).next();

 

Well, we have stored the name entered by the user into a variable named name. Next, how to use it in a print statement? 

It's actually very simple, just replace jason with the variable name, such as

System.out.println("Hello" + name);

Note that we replaced "Hello Jason" in parentheses with "Hello"+name, where the + sign indicates the concatenation of the strings.

 

Well, let's run the modified program and have a look.

 

You will notice that the program is waiting for user input:

When you type any name and hit enter, the program says hello to the world and that name:

jason

Hello world

Hello jason

 

In this way, our program is much smarter. After it knows your name, it will use your name to say hello to you.

 

Let's review what we learned in this lesson.

First, through the second print statement, we know that the Java statements are executed sequentially, and the statements are separated by ;.

Secondly, we also know that the user's keyboard input can be obtained through (new Scanner(System.in)).next()

More importantly, we know how to define a variable, how to use the variable's assignment statement = to store data into the variable, and how to use the variable

Finally, we know that strings can be concatenated by the + sign

 

So far we can use Java,

1. Get user input

2. Store the user's input in a variable

3. The result can be printed and output to the user

 

Next we will learn, in Java, what data types are there variables, and what basic operations Java supports

ok, see you next time, byebye

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327008509&siteId=291194637