跳转至

4.0 Loop

Switch

import java.util.Scanner;

public class Switch {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        switch (num) {
            case 1:
                System.out.println("One");
                break;
            case 2:
                System.out.println("Two");
                break;
            case 3:
                System.out.println("Three");
                break;
            default:
                System.out.println("Other");
        }
    }
}

While

import java.util.Scanner;

public class Break {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("Are you ready for this program to end? Enter true or false: ");
            boolean answer = scanner.nextBoolean();
            if (answer)
                break;
        }
    }
}

Do-while loops

import java.util.Scanner;

public class DoWhile {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean answer; // Note that this variable is declared outside the loop.
        do {
            System.out.print("Are you ready for this program to end? Enter true or false: ");
            answer = scanner.nextBoolean();
        } while (!answer);
    }
}

无论是 Python 还是 Java 都是当 While 中的条件满足时继续执行该 Loop

while (i <=10) 当 i 小于 10 时继续执行该循环,或直接当这个条件为 true 时

Empty statements

public class EmptyStatement {
    public static void main(String[] args) {
        ;;; // Empty statements
        int x = 10;
        for (x = 0; x < 3; x++);
        System.out.println("x=" + x);
    }
}

在 for 循环中,分号表示循环体为空,循环的条件满足时并不会执行任何操作。

循环完后,x 的值会被打印出来。

这种写法在需要的情况下保持代码的简洁性,特别在循环中不需要任何额外操作时。

import java.util.Scanner;

public class EmptyForLoop {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        for (; x > 0; --x)
            System.out.println("x=" + x);
    }
}

For-Each loops

for (int num : nums) {
    System.out.println(num);
}

Continue

continue ends execution early and goes to the next iteration

public class Continue {
    public static void main(String[] args) {
        for (int i = 1; i <= 20; i++) {
            if (i % 2 == 0)
                continue;
            System.out.println(i);
        }
    }
}

OOP

同一文件夹下可以自动识别路径,其他文件路径需要导入

javac BankExample.java
java BankExample

编译器会自动寻找相关 class 最终直接执行 Main 所在的 class 即可得到结果

return

class BankAccount3 {
    String ownerName;
    int balance;

    void depositMoney(int amount) {
        if (amount < 0) {
            System.out.println("You can't deposit a negative amount!");
            return;
        }
        balance += amount;
    }
}

return 会直接结束方法的执行,类似于 break

Constructor

class BankAccount5 {
    String ownerName;
    int balance;

    BankAccount5() {
        ownerName = null;
        balance = 0;
    }
}

类当作设计图纸,模具,对象 object / instance 当作实际制造出来的东西

而构造器用于对象的初始化过程,工厂用其准备好初始生产步骤

  • new 关键字创建新对象的时候被自动调用
  • 可以有多个参数不同的 constructor
  • 没有返回类型
  • 没写 Java 也会给你一个啥也不做无参数隐藏的默认构造器,写了就不再提供

this

class BankAccount6 {
    String ownerName;
    int balance;

    BankAccount6(String ownerName, int balance) {
        this.ownerName = ownerName;
        this.balance = balance;
    }
}

this 关键词用于区分 parameters 和 field

  • field 就是 class 的属性
  • parameters 参数
  • this.name always means a field; 把用户传经来的参数保存在字段里