时间:
内容:
switch-case的用法举例
package study; import java.util.Scanner; public class helloworld { public static void main(String[] args) { Scanner in =new Scanner(System.in); int type = in.nextInt(); switch (type) { case 1: case 2: System.out.println("你好"); break; case 3: System.out.println("陌生人"); break; default: System.out.println("Sorry"); break; } } }
判断语句的用法举例
package study; import java.util.Scanner; public class helloworld { public static void main(String[] args) { System.out.println("请输入你的年龄"); Scanner in =new Scanner(System.in); int MIRROR = 35; int age = in.nextInt(); if( age < MIRROR) { System.out.println("你还很年轻"); } else if (age > MIRROR) { System.out.println("请珍惜剩余的时光"); } else { System.out.println("happy birthday"); } } }
常见的错误示范:
if 和 else语句后面必须要跟上一个大括号形成语句块。 小技巧:control键+ / 可以直接让代码注释。 我是更喜欢用if else不太喜欢用switch-case的用法