[JavaSE] 逻辑控制

在这里插入图片描述
switch语句

  • 根据 switch 中值的不同, 会执行对应的 case 语句. 遇到 break 就会结束该 case 语句.
  • 如果 switch 中的值没有匹配的 case, 就会执行 default 中的语句.
  • 我们建议一个 switch 语句最好都要带上 default.

我们看一个例子

public class Solution {
    public static void main(String[] args) {
        System.out.println("value="+switchit(4));
    }
    public static int switchit(int x) {
        int j=1;
        switch (x) {
            case 1:j++;
                break;
            case 2:j++;
                break;
            case 3:j++;
                break;
            case 4:j++;
                break;
            case 5:j++;
                break;
            default:j++;
                break;
        }
        return j+x;
    }
}

在这里插入图片描述
如果把break去掉,我们再来看

public class Solution {
    public static void main(String[] args) {
        System.out.println("value="+switchit(4));
    }
    public static int switchit(int x) {
        int j=1;
        switch (x) {
            case 1:j++;
            case 2:j++;
            case 3:j++;
            case 4:j++;
            case 5:j++;
            default:j++;
        }
        return j+x;
    }
}

在这里插入图片描述
结果和之前不一样,原因是因为不写 break 的时候, case 语句会依次向下执行, 从而失去了多分支的效果.
while循环
注意:

  1. 和 if 类似, while 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { }
  2. 和 if 类似, while 后面的 { 建议和 while 写在同一行.
  3. 和 if 类似, while 后面不要多写 分号, 否则可能导致循环不能正确执行
    for循环
    计算 1!+2!+3!+4!+5!
public class Solution {
    public static void main(String[] args) {
        int sum = 0;
        for(int i = 0;i <= 5;i++) {
            int temp = 1;
            for(int j = 1;j <= i;j++) {
                temp *= j;
            }
            sum += temp;
        }
        System.out.println("sum =" + sum);
    }
}

注意事项 (和while循环类似)

  1. 和 if 类似, for 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { }
  2. 和 if 类似, for 后面的 { 建议和 while 写在同一行.
  3. 和 if 类似, for 后面不要多写 分号, 否则可能导致循环不能正确执行
    do while循环
do {
   循环语句
}while(判断条件)

先执行循环语句, 再判定循环条件。
更推荐for 或 while

输出到控制台

System.out.println(msg); // 输出一个字符串, 带换行
System.out.print(msg); // 输出一个字符串, 不带换行
System.out.printf(format, msg); // 格式化输出 

从键盘输入

- 读入一个字符
直接使用 System.in.read 可以读入一个字符. 但是需要搭配异常处理。

import java.io.IOException;
public class Solution {
    public static void main(String[] args) {
        try {
            System.out.print("Enter a Char:");
            char i = (char) System.in.read();
            System.out.println("your char is :"+i);
        } catch (IOException e) {
            System.out.println("exception");
        }
    }
}

比较麻烦, 我们不推荐使用

- 使用 Scanner 读取字符串/整数/浮点数


import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的名字:");
        String name = sc.nextLine();
        System.out.println("请输入你的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入你的工资:");
        float salary = sc.nextFloat();
        System.out.println("你的信息如下:");
        System.out.println("姓名:"+name+"\n"+
        	"年龄:"+age+"\n"+"工资:"+salary);
        sc.close();
    }
}
  • 使用 Scanner 循环读取 N 个数字
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double sum = 0.0;
        int num = 0;
        while (sc.hasNextDouble()) {
            double tmp = sc.nextDouble();
            sum += tmp;
            num++;
        }
        System.out.println("sum = " + sum);
        System.out.println("avg = " + sum / num);
        sc.close();
    }
}

在这里插入图片描述
注意事项: 当循环输入多个数据的时候, 使用 ctrl + z 来结束输入 (Windows 上使用 ctrl + z, Linux / Mac 上使用 ctrl +
d)

发布了60 篇原创文章 · 获赞 23 · 访问量 3314

猜你喜欢

转载自blog.csdn.net/weixin_44945537/article/details/104033683