Java branch structure, loop structure 2020 11 25

Select the structure
if syntax:

if(布尔表达式){
    
    
	//如果布尔表达式为true
}
else{
    
    
	//如果布尔表达式为false
}

if single branch structure

//判断学生成绩是否及格
import java.util.Scanner;
public class 流程控制{
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        double scor=scanner.nextDouble();
        if (scor>=60){
    
    
            System.out.println("及格");
        }else{
    
    
            System.out.println("不及格");
        }
        scanner.close();
    }
}

if multiple choice structure
usage is the same as c language

import java.util.Scanner;
public class 流程控制{
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        double scor=scanner.nextDouble();
        if (scor>=90){
    
    
            System.out.println("A级");
        }else if(scor>=80&&scor<90){
    
    
        	System.out.println("B级");
        }else if(scor>=70&&scor<80){
    
    
        	System.out.println("c级");
        }else if(scor<70&&scor>=60){
    
    
        	System.out.println("d级");
        }else{
    
    
            System.out.println("不及格");
        }
        scanner.close();
    }
}

The switch multi-selection structure
is the same as the c language. Support int char string;

import java.util.*;
public class 流程控制{
    
    
    public static void main(String[] args) {
    
    
        char grad= 'b';
        switch (grad){
    
    
            case'a':
                System.out.println("优秀");
                break;
            case'b':
                System.out.println("及格");
                break;
            case'c':
                System.out.println("再接再厉");
                break;
            case'd':
                System.out.println("继续努力");
                break;
            default:
                System.out.println("输出错误");
                break;
        }
    }
}

Loop structure
while, do...while, for

while(布尔表达式){
    
    
	//循环内容
}

Find the sum of all numbers in 1-100

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

    }
}
5050

Process finished with exit code 0

for loop The
for loop statement supports a general structure of iteration, and is the most effective and flexible loop structure.
for syntax

	for(初始化;布尔表达式;更新){
    
    
		//代码语句
	}

Exercise 1: Calculate the sum of odd and even numbers between 0 and 100.
Exercise 2: Use a while or for loop to output numbers that are divisible by 5 between 1 and 1000, and output 3 per line.
Exercise 3: Print a nine-nine-nine multiplication table

//***练习一:计算0到100之间的奇数和偶数的和***
public class 流程控制 {
    
    
    public static void main(String[] args) {
    
    
         int sum=0;
        for(int i=0;i<=100;i+=2){
    
    
            sum+=i;
        }
        System.out.println(+sum);
    }
    }
}
//练习二:***用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个***
public class enen {
    
    
    public static void main(String[] args) {
    
    
        int t=0;
        for(int a=5;a<=1000;a++){
    
    
            if(a%10==5||a%10==0){
    
    
                System.out.println(+a);
                t++;
                if(t==3){
    
    
                    System.out.println("\n");
                    t=0;
                }
            }
        }
    }
}

println()
output will wrap completely
print()
output will not wrap

//练习三:打印九九乘法表
public class enen {
    
    
    public static void main(String[] args) {
    
    
        for(int i=1;i<=9;i++){
    
    
            for(int j=1;j<=i;j++){
    
    
                int sum=j*i;
                System.out.print(j+"*"+i+"=" +(i*j)+"\t");
            }
            System.out.print("\n");
        }
    }
}


1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	

Process finished with exit code 0

Break, continue statement
In the main part of any loop statement, break can be used to control the flow of the loop. Break is used to forcibly exit the loop without executing the remaining statements in the loop. (The break statement is also used in the switch statement) The
continue statement is used in the loop statement body to terminate a loop process, that is, skip the statement that has not been executed in the loop body, and then determine whether to execute the loop next time.

//break
public class enen {
    
    
    public static void main(String[] args) {
    
    
        for(int i=1;i<=10;i++){
    
    
            if(i==5){
    
    
                
                break;
            }
            System.out.println(+i);

        }
    }
}
//结果(当i==5时,break,结束循环
1
2
3
4

Process finished with exit code 0

public class enen {
    
    
    public static void main(String[] args) {
    
    
        for(int i=1;i<=10;i++){
    
    
            if(i==5){
    
    
                System.out.println();
                continue;
            }
            System.out.println(+i);

        }
    }
}

//结果(当i==5时,continue,跳过本次循环,进行下一次循环,所有结果没有5
1
2
3
4

6
7
8
9
10

Exercise 4: Printing triangles

//练习四:打印三角形

public class three {
    
    
    public static void main(String[] args) {
    
    
        for(int i = 1; i <= 5; i++) {
    
    

            for(int k = 1; k <= 5 - i; k++) {
    
    
                System.out.print(" ");
            }
            for(int k = 1; k <= 2 * i - 1; k++) {
    
    
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

    *
   ***
  *****
 *******
*********

Guess you like

Origin blog.csdn.net/qq_52044923/article/details/110135611