Java SE Quick Start--2

Process control

The flow control in java se is basically the same as its C language. Here we will talk about if first.

if_else if_else

if(条件){
    //执行的内容
}else if(条件){
    //执行的内容
}else{
    //执行的内容
}

If the condition is true, the content in the corresponding code block is executed. In addition, let's think about it, if both if elseif and else are replaced by if.

if(条件){
    //执行的内容
}
if(条件){
    //执行的内容
}
if(条件){
    //执行的内容
}

The same effect can be achieved, but in if_else if_else, if one is established, other conditions will not be judged, and in the above case, if if is established, the following content will still be executed.

switch

The use of switch is usually to determine some fixed value, for example, enter a number, the judgment is the day of the week.

import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        String str = "";
        switch(i){
        case 1:
            str = "星期一";
            break;
        case 2:
            str = "星期二";
            break;
        case 3:
            str = "星期三";
            break;
        case 4:
            str = "星期四";
            break;
        case 5:
            str = "星期五";
            break;
        case 6:
            str = "星期六";
            break;
        case 7:
            str = "星期日";
            break;
        default:
            str = "请输入正确的数字";
            break;
        }
        System.out.println(str);
    }
}

When we judge with a fixed value, we can use switch. In addition, switch also supports char, short, byte, and jdk1.7 has added String. Switch is often used with enumeration. What is enumeration, we will talk about it later .
default in switch is not necessary, if you try to put defualt first, it will not execute what is in default if the condition in case is met.

loop structure

The do_while loop, the meaning of the loop, is executed once, and then judged by the conditions in the while

        int i = 1;
        do{
            i = i + 1;
        }while(i<100);

while loop

while(条件){
    //循环的内容
}

The difference between this loop and the do while loop is that the loop needs to judge the conditions first, and if the conditions are not met, it will exit the loop
//
for loop

for(初始化;退出循环的条件;每次循环必执行的语句){
    //循环的内容
}

For example, a common loop that outputs 100 hellos

for(int i = 0;i<100;i++){
    System.out.println("hello");
}

In fact, whether it is a while loop or a do while loop, it can be transformed into a for loop.

automatic type conversion

char->short
byte->short
short->int->float->double
short->int->long
low byte type can be converted to high byte type.
If you want to convert the type of the high byte to the type of the low byte, you need to use forced type conversion, such as converting double to int, then you can directly use double c = 1.23; int a = (int)c;
In java, Can't convert boolean type to int type.

++ i given i ++

First look at i++

int i = 1;
int a = i++;
System.out.println(a);

i++ means that i participates in the operation first, and then ++, so here, the output is 1, which can be expressed as the following situation, so the last i is 2

int i = 1;
int a = i;
i = i + 1;
System.out.println(a);

++i again

int i = 1;
int a = ++i;
System.out.println(a);

++i, let i increase by itself, and let i participate in the operation, so output 2 here, which can be expressed as the following situation

int i = 1;
i = i + 1;
int a = i;
System.out.println(a);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325463873&siteId=291194637