Java essential knowledge point 2 - input and output and variables

  1. variable

  1. The substance of the variable

If memory is compared to a house, storing in memory is equivalent to living in a house. But if a room is given to the respondent, but he does not live in it, then the room is actually empty and belongs to the individual, and others cannot live in it, which will cause waste.

Then compare the memory to a hotel, and the people who live in the house are called constants and variables according to the length of the house and other needs...

A guest named Xiao Ming lived in room 001, so the actual address is No. 001, but when we usually call the room name, we will call it Xiao Ming's house.

  1. The use of variables: Consistent with other languages, declaration → assignment → use.

Constants: just add final in front (C language adds const in front), naming rules: usually all uppercase, for example: final int AGE = 18; or final int X_AGE = 18;

  1. Variable scope:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        int num = 2;//num被声明在main方法里,作用域是整个main方法。
        if(num == 2) {
            int num1 = num * num;//num1是声明在if语句中,作用域是if块
            System.out.println("num和1的值分别是:"+num + num1);
        }
        System.out.println("num和1的值分别是:"+num + num1);//会报错,原因:num1是声明在if语句中,此时num1已经被释放。
    }
  1. type conversion

Same as C language.

  1. Scanner class

Function: keyboard input function

The Scanner class is located in the Java util package, which needs to be imported when used, and the Scanner tool class is used to obtain the information entered by the user.

Steps: 1. Import java.util.Scanner >> import java.util.Scanner;

2. Create a Scanner object. Scanner input = new Scanner(System.in);

 The rule is Scanner name = new Scanner(System.in);

3. Accept and save the value entered by the user.

example:

package text;

import java.util.Scanner;

public class Text {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan =new Scanner(System.in);
        System.out.println("请输入你的名字:");//这里会卡住,等待你的输入
        String name = scan.next();//注意要声明一个变量,变量值由键盘输入。
        System.out.println("你的名字是:" +name);
    }
}

But it is not used in later server programming. You only need to know how to use it, and you don't need to pay too much attention. Just remember the above code example, there is not too much usage.

Among them, if it is an integer, it is .nextInt(), if it is a decimal double, it is .nextDouble(), if it is a character type, it is next(). There are only these three usages.

  1. Output rules:

  1. The things you want to output directly should be wrapped with "", for example, System.out.println("Hello"); will output Hello

  1. Multiple things to be output can be spliced ​​together using "+". Example int age = 18; such as: System.out.println("age: " + age);

If you want to do calculations in the output and want to increase the priority, you only need to enclose it in parentheses to increase the priority.

System.out.println("Difference between two numbers: "+(ab));

System.out.println("Sum of two numbers: "+(a+b));

Guess you like

Origin blog.csdn.net/weixin_48060069/article/details/129103546