コントロールの構造と機能Java--

まず、構造を選択します。

  • IF(ブール式)単一ケース
  • (ブール式)の場合例...他に2種類
  • IF(ブール値)であれば、他の(ブーリアン)... ...例他にすることができ、さまざまな

複数:スイッチ(表現)

  • その他のケースブランチ
  • 枝を満たした後、我々は破るために必要
  • デフォルトへの最後の分岐

第二に、ループ構造

  • 同時に
  • 一方を行います
  • ために
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();
    }
}

割り込み制御フロー文

  • 現在のサイクルのうち、実行ループの背後に配置され、ブレーク
  • 継続:;文の部分を更新する場合は、ループ変数のスキップを制御する通常の流れを中断し、制御は、最も内側のループのヘッド部に転送されます
  • ラベル付きbreak文は:複数のループの外にジャンプするような文を後藤;タグは、希望のうち、最も外側のループの前に置かれ、コロンを続けなければなりません
  • ラベルの最初の試合に循環ジャンプ:ラベル付きステートメントを続けます
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

第三に、機能

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

  • この関数は、クラスの範囲内に配置する必要があります。通常の状況下では、提案した方法が公開されています
  • 関数は、上記の例のような他の機能を呼び出すことができ、メイン関数呼び出しは、機能を追加します
  • 再帰関数呼び出しは、我々は終了条件に注意を払う必要があります
  • 同じクラス、関数名が同じ、すなわち、オーバーロード関数(過負荷)であることができるが、関数の引数の数や種類が異なっていなければなりません。あなたは、同じ名前の関数を区別するために値を返すことはできません。
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;
        }
    }
}

おすすめ

転載: www.cnblogs.com/xxwang1018/p/11607032.html