0
点赞
收藏
分享

微信扫一扫

学习java第二天(2/2)

单调先生 2022-02-24 阅读 47

时间:

内容:


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的用法
举报

相关推荐

java学习 第二天

JAVA学习第二天

java第二天

Java第二天

JAVA第二天

Kotlin学习第二天

RHCSA学习第二天

0 条评论