JAVA-Basic Grammar

Zero, basic concepts

8 basic data types: integer 4, floating point 2, character, Boolean

1. Operation

Arithmetic operators + - * / %

cast: (type to be converted) original data

double a = 12.3;
int b = 1 + (int) a; //b=13

Self-increment and self-decrement operations: ++ --

int a = 10;
int b = a++; //先计算再加1 b=10 a=11

int c = 10;
int d = ++c; //先加1再计算 d=11 c=11

a += 1;
a -= 1;

 二、if switch for while

1. If conditional judgment

Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
int a = sc.nextInt();
if (a > 0 && a <= 10) {
    System.out.println("a是一个比较小的数:" + a);
} else if (a > 10) {
    System.out.println("a是一个比较大的数:" + a);
} else {
    System.out.println("a不合法");
}

2. Switch condition judgment does not write break will lead to case penetration

int number = 100;
switch (number){
    case 1:
        System.out.println("number的值为1");
        break;
    case 10:
        System.out.println("number的值为10");
        break;
    default:
        System.out.println("number的值不是1或者10");
        break;
}

3. for loop

continue: end this cycle and enter the next cycle

break: end the entire loop

for (int i = 1; i <= 10; i++){
    if(i==3) continue;
    if(i==6) break;
    System.out.println("现在打印的是" + i);
}

4. while loop

while(i <= 100){
    System.out.println(i);
    i++;
}

Guess you like

Origin blog.csdn.net/m0_58285786/article/details/129867807