Structure and function of the control Java--

First, select the structure:

  • if (boolean expression) a single case
  • if (boolean expression) ... else two kinds of case
  • if (Boolean) ... else if (Boolean) ... else can a variety of case

Multiple: switch (expression)

  • More case branch
  • After satisfying a branch, we need to break
  • The last branch to default

Second, the loop structure

  • while
  • do…while
  • for
package Primary;

/**
 * Java控制结构
 * @version 15:21 2019-09-29
 * @author xxwang1018
 */

class IfElseTest { //选择结构之if-else
    public void i2() {
        int a = 5;
        if(a>1) {
            System.out.println("aaaaaaa");
        }

        if(a>10) {
            System.out.println("bbbbbbb");
        } else {
            System.out.println("ccccccc");
        }

        if(a>10) {
            System.out.println("dddddd");
        }
        else if(a>=5) {
            System.out.println("eeeeee");
        }
        else {
            System.out.println("ffffff");
        }
    }
}
class SwitchTest { //选择结构之switch
    public void s1() {
        int a1 = 1;
        int a2 = 2;
        switch(a1+a2) {
            case 1: System.out.println("aaaaaaa");
                break;
            case 2: System.out.println("bbbbbbb");
                break;
            case 3: System.out.println("ccccccc");
                //break;
            default:System.out.println("ddddddd");
        }

        String b = "abc";
        switch(b) {
            case "abc": System.out.println("eeeeeee");
                break;
            case "def": System.out.println("fffffff");
                break;
            case "hgi": System.out.println("ggggggg");
                break;
            default:System.out.println("hhhhhhh");
        }
    }
}
class WhileTest { //循环结构之while
    public void w1() {
        System.out.println("=============While Test==========");
        int x = 10;
        while (x < 20) {
            System.out.print("value of x : " + x);
            x++;
            System.out.print("\n");
        }

        System.out.println("=============Do  While Test==========");
        x = 10;
        do {
            x++;
            if(x%2 == 0) {
                continue;
            }
            System.out.println("value of x : " + x);
        } while (x < 20);
    }
}
class ForTest { //循环结构之for
    public void f2() {
        for(int i=0;i<5;i++) {
            for(int j=0;j<5;j++) {
                if(j<=i) {
                    System.out.print("*");
                } else {
                    break;
                }
            }
            System.out.println();
        }
    }
}

public class ControlStructure {
    public static void main(String[] args) {
        new IfElseTest().i2();
        new WhileTest().w1();
        new ForTest().f2();
        new SwitchTest().s1();
    }
}

Interrupt control flow statements

  • break: out of the current cycle is located behind the execution loop
  • continue: interrupting the normal flow of control, the control will be transferred to the head section of the innermost loop; if for updating portion for statement, the loop variable skip
  • break statement with label: goto a similar statement to jump out of multiple loops; tag must be placed before the outermost loop out of hope, and followed by a colon
  • continue labeled statement: circulation jump to the label's first match
import java.util.*;
import java.math.*;
 
/**
 * This program discribes special "break" statements
 * @version 15:40 2019-03-27
 * @auther xxwang1018
 */
 
public class SpecialBreak{
    public static void main(String[] args) {
        int[] score = new int[10];
        int total=0;
        for (int i = 0; i < 10; ++i){
            score[i] = (int) (1 + Math.random() * 100); //随机产生学生分数
            total+=score[i]; //计算总分
        }
        double average=1.0*total/10; //计算平均分
        int counter=0;
        /**
         count 用来记录第一个低于平均分的学生的序号,且必须要初始化
         */
 
        read_data:
        while(average<total){
            for(int i=0; i<10; ++i){
                if(score[i]>average)
                    System.out.println("Congratulations! You are ok!"); //对超过平均分的学生鼓励
                else{
                    System.out.println("\nThe first student who doesn't get an average score appears.");
                    counter=i;
                    break read_data;
                }
                System.out.println(score[i]);
            }
        }
        System.out.println("\nThe average is "+average+"\nThis student's score is "+score[counter]);
    }
}

/*
测试结果:
Congratulations! You are ok!
92
Congratulations! You are ok!
85
 
The first student who doesn't get an average score appears.
 
The average is 61.6
This student's score is 39

Third, function

修饰词(public 或者 static) 返回值(int 或者void),函数名(形 参列表) {函数体}

  • Function must be placed in the range of classes. Under normal circumstances, the proposed method is public
  • Function may call other functions, such as the above example, the main function calls add function
  • Recursive function calls, we need to pay attention to the termination condition
  • Same class, function name can be the same, i.e. overloaded functions (overload), but the number or type of the function arguments must be different. You can not return a value to distinguish the function of the same name
package Primary;

/**
 * Java函数
 * @version 15:42 2019-09-29
 * @author xxwang1018
 */

public class Function {
    public static void main(String[] args) {
        int a = 5;
        int b = factorialCalculation(a);
        System.out.println("The factorial of " + a + " is " + b);
    }

    public static int factorialCalculation(int m) {
        if (m > 1) {
            return m * factorialCalculation(m - 1);
        } else {
            return 1;
        }
    }
}

Guess you like

Origin www.cnblogs.com/xxwang1018/p/11607032.html