Switch的简单应用

/*
键盘录入字符串,根据给定的字符串,来输出你选择的字符串是什么?(表达式是字符串的情况)
满足要求,就输出
否则,提示有误。

String s = sc.nextLine();  字符串类型

*/

import java.util.Scanner;
class SwitchTest3{
	public static void main(String[] args){
		//创建键盘录入对象
		Scanner sc = new Scanner(System.in);
	
	//录入数据
	System.out.println("请输入你要判断字符串:");
	String s = sc.nextLine();   //字符串类型sc.nextLine
	
	switch(s) {
		case "hello":
			System.out.println("您输入的是hello");
			break;
		case "world":
			System.out.println("您输入的是world");
			break;
		case "java":
			System.out.println("您输入的是java");
			break;
		default:
			System.out.println("没有找到你输入的数据");
			break;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42322406/article/details/89478835