Java 基础之流程控制

快来打我* 2023-09-29 18:28 59阅读 0赞

1. 输入输出

输入

  1. System.out.print() // 不换行输出
  2. System.out.println() // 换行输出

格式化输出

System.out.printf() 结合占位符输出想要的格式,常见占位符:

  • %d:格式化输出整数
  • %x:格式化输出十六进制整数
  • %f:格式化输出浮点数
  • %e:格式化输出科学计数法表示的浮点数
  • %s: 格式化字符串,不知道用什么占位符,都可以用 %s

    // 保留两位有效小数
    System.out.printf(“%.2f”, 2.678); // 2.68
    System.out.printf(“Hello, %s”, “rose!”); // Hello, rose!

输入

  1. // Control.java
  2. import java.util.Scanner;
  3. public class Control {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in); // 创建 Scanner 对象
  6. System.out.print("Input your name: ");
  7. String name = scanner.nextLine(); // 获取终端输入(字符串)
  8. System.out.print("Input your age: ");
  9. int age = scanner.nextInt(); // 获取终端输入(int 类型)
  10. System.out.printf("Hi, %s, you are %d\n", name, age);
  11. }
  12. }

标准输入需要在终端编译并输入:

  1. F:\JavaStudy\Source\Hello World>javac Control.java
  2. F:\JavaStudy\Source\Hello World>java Control
  3. Input your name: rose
  4. Input your age: 18
  5. Hi, rose, you are 18

注意:Scanner 对象会自动转换数据类型,无需手动转换!

2. if 语句

1、使用整型来作为判断条件:

  1. // 不要使用 80 <= score < 90
  2. int score = 98;
  3. if (score >= 90) {
  4. System.out.println("A");
  5. } else if (score >= 80 & score < 90) {
  6. System.out.println("B");
  7. } else if (score >= 70 & score < 80) {
  8. System.out.println("C");
  9. } else if (score >= 60 & score < 70) {
  10. System.out.println("D");
  11. } else {
  12. System.out.println("E");
  13. }

2、浮点型作为判断条件:

  1. // 与某个数值的差的绝对值小于某个很小的数
  2. double x = 1 - 9.0 / 10;
  3. if (Math.abs(x-0.1) < 0.00001) {
  4. System.out.println("x is 0.1");
  5. } else {
  6. System.out.println("x is not 0.1");
  7. }

浮点型应利用差值小于某个临界值来判断,而不是直接判断。


判断引用类型是否相等

  • ==:标准数据类型判断的是值是否相等,引用类型判断的是否为同一指向(即是否为同一对象)
  • 引用类型值是否相等:使用 equals()

    // 判断引用类型是否相等
    String a = “love”;
    String b = “LOVE”.toLowerCase(Locale.ROOT);
    if (a == b) {

    1. System.out.println("a == b");

    } else {

    1. System.out.println("a != b"); // a != b

    }

    if (a.equals(b)) {

    1. System.out.println("a == b"); // a == b

    } else {

    1. System.out.println("a != b");

    }

使用短路语句避免 null 类型时编译错误:

  1. // 若 a != null 为假则不会计算 a.equals(b),从而避免 NullPointerException
  2. String a = null;
  3. String b = "LOVE".toLowerCase(Locale.ROOT);
  4. if (a != null && a.equals(b)) {
  5. System.out.println("a == b");
  6. } else {
  7. System.out.println("a != b"); // a != b
  8. }

3. switch 多重判断

switch 可以根据某个表达式的结果,分别去执行不同的分支。

  • 遇到 break 才会结束,切记不能忘记
  • 若没有满足的条件,可以定义 default,输出默认值

    // switch 多重判断
    int salary = 20000;
    switch (salary){

    1. case 15000:
    2. System.out.printf("rose salary is %d", salary);
    3. break;
    4. case 18000:
    5. System.out.printf("lila salary is %d", salary);
    6. break;
    7. case 19000:
    8. System.out.printf("john salary is %d", salary);
    9. break;
    10. default:
    11. System.out.printf("My salary is %d", salary);
    12. break;

    }

如果有几个case语句执行的是同一组语句块,可以这么写:

  1. int salary = 20000;
  2. switch (salary) {
  3. case 15000:
  4. System.out.printf("rose salary is %d", salary);
  5. break;
  6. case 18000:
  7. case 19000:
  8. System.out.printf("john salary is %d", salary);
  9. break;
  10. default:
  11. System.out.printf("My salary is %d", salary);
  12. break;
  13. }

比较字符串:

  1. // 比较字符串是比较值是否相等
  2. String fruit = "apple";
  3. switch (fruit) {
  4. case "apple":
  5. System.out.println("Selected apple");
  6. break;
  7. case "pear":
  8. System.out.println("Selected pear");
  9. break;
  10. case "mango":
  11. System.out.println("Selected mango");
  12. break;
  13. default:
  14. System.out.println("No fruit selected");
  15. break;
  16. }

4. while 循环语句

基本格式:

  1. while (条件表达式) {
  2. 循环语句
  3. }

计算 1+2+3+100

  1. int n = 0;
  2. int sum = 0;
  3. while (n <= 100) {
  4. sum += n;
  5. //n += 1;
  6. n++; // n 自增 1 ,上述两种方式都可以
  7. }
  8. System.out.println(sum); // 5050

死循环:

  1. int n = 1;
  2. int sum = 0;
  3. while (n > 0) {
  4. sum += n;
  5. n++;
  6. }
  7. System.out.println(sum); // -1073741824
  8. System.out.println(n); // -2147483648

因为 int 有最大值,当 n++ 一值自增超出 int 范围就会意外退出循环。


do while 循环

do...while 循环至少会执行一次循环语句,再判断,而 while 循环线判断再执行循环语句:

  1. do {
  2. 执行循环语句
  3. } while (条件表达式);

计算 1+2+3+100

  1. int n = 1;
  2. int sum = 0;
  3. do {
  4. sum += n;
  5. n++;
  6. } while (n <= 100);
  7. System.out.println(sum);

5. for 循环

  1. for (初始条件; 循环检测条件; 循环后更新计数器) {
  2. // 执行语句
  3. }

计算 1+2+3+100

  1. int sum = 0;
  2. for (int n = 1; n <= 100; n++) {
  3. sum += n;
  4. }
  5. System.out.println(sum);

for 循环遍历数组:

  1. int[] arr1 = {
  2. 90, 92, 95, 97, 99};
  3. for (int n = 0; n < arr1.length; n++){
  4. System.out.println(arr1[n]);
  5. }

for each 循环

for each 循环比 for 循环更适合用来遍历数组:

  1. // for each 循环
  2. int[] arr1 = {
  3. 90, 92, 95, 97, 99};
  4. for (int n : arr1) {
  5. System.out.println(n);
  6. }

注意:除了数组外,for each循环能够遍历所有“可迭代”的数据类型,包括ListMap等。

6. break 和 continue

  • break:退出循环,通常都是配合if语句使用
  • continue:退出当前(本次)循环

1、当 n==20 时退出循环:

  1. int sum = 0;
  2. for (int n = 1; n <= 100; n++) {
  3. sum += n;
  4. if (n == 20) {
  5. break;
  6. }
  7. }
  8. System.out.println(sum); // 210

2、多层循环中使用 break 语句:

  1. for (int i = 0; i <= 10; i++) {
  2. for (int j = 0; j <= 10; j++) {
  3. System.out.println("j ===> " + j);
  4. if (j == i) {
  5. break; // 只会退出内存循环,然后重新回到外层循环继续循环
  6. }
  7. }
  8. System.out.println("内存循环退出!");
  9. }

运行结果:

  1. j ===> 0
  2. 内存循环退出!
  3. j ===> 0
  4. j ===> 1
  5. 内存循环退出!
  6. j ===> 0
  7. j ===> 1
  8. j ===> 2
  9. 内存循环退出!
  10. j ===> 0
  11. j ===> 1
  12. j ===> 2
  13. j ===> 3
  14. 内存循环退出!
  15. j ===> 0
  16. j ===> 1
  17. j ===> 2
  18. j ===> 3
  19. j ===> 4
  20. 内存循环退出!
  21. j ===> 0
  22. j ===> 1
  23. j ===> 2
  24. j ===> 3
  25. j ===> 4
  26. j ===> 5
  27. 内存循环退出!
  28. j ===> 0
  29. j ===> 1
  30. j ===> 2
  31. j ===> 3
  32. j ===> 4
  33. j ===> 5
  34. j ===> 6
  35. 内存循环退出!
  36. j ===> 0
  37. j ===> 1
  38. j ===> 2
  39. j ===> 3
  40. j ===> 4
  41. j ===> 5
  42. j ===> 6
  43. j ===> 7
  44. 内存循环退出!
  45. j ===> 0
  46. j ===> 1
  47. j ===> 2
  48. j ===> 3
  49. j ===> 4
  50. j ===> 5
  51. j ===> 6
  52. j ===> 7
  53. j ===> 8
  54. 内存循环退出!
  55. j ===> 0
  56. j ===> 1
  57. j ===> 2
  58. j ===> 3
  59. j ===> 4
  60. j ===> 5
  61. j ===> 6
  62. j ===> 7
  63. j ===> 8
  64. j ===> 9
  65. 内存循环退出!
  66. j ===> 0
  67. j ===> 1
  68. j ===> 2
  69. j ===> 3
  70. j ===> 4
  71. j ===> 5
  72. j ===> 6
  73. j ===> 7
  74. j ===> 8
  75. j ===> 9
  76. j ===> 10
  77. 内存循环退出!

3、continue 语句:

  1. int sum = 0;
  2. for (int n = 1; n <= 10; n++) {
  3. // 当 n 为偶数时,跳出当前循环,继续下一次循环,因此计算的是 1+3+5+7+9 = 25
  4. if (n % 2 == 0) {
  5. continue;
  6. }
  7. sum += n;
  8. }
  9. System.out.println(sum); // 25

发表评论

表情:
评论列表 (有 0 条评论,59人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java基础Java流程控制

    > 块(即复合语句)是指由一对大括号括起来的若干条简单的 Java 语句。块确定了变量的作用域。一个块可以嵌套在另一个块中。但是,不能在嵌套的两个块中声明同名的变量。使用块(有

    相关 Java基础流程控制

    上次的运算符都消化好了吗?每一天都要用到一些哦~ 以前有提到过一嘴,程序执行都是从上到下执行的,emm,学到这里,感觉这句话是对的也是错的了…… 如果都是一行一行执行下去的