Java中switch的三种用法详解:
switch居然有三种方式 ? 作为一个接触java不久的人来说,这确实让我吃了一惊!
根据版本,在java14开始, switch语句有了一个很大的调整, 这就让swicth语句有了更多的操作和选择,在代码上,更加的简便灵活, 让我们试试这神奇的switch吧!
使用switch这个关键词, 我们可以很好的解决if…else 中多重选择的尴尬场面!
Java中switch的三种用法详解:
switch 标准方式
switch - > 用法:
switch yield 用法:
switch 标准方式
flag 表示计算结果, 必须是整性, 字符串类型, 或者枚举类型。
1
2
3
4
5
6
7
8
9
switch
(flag){
case
0
: 语句一;
break
;
case
1
: 语句二;
break
;
default
: 语句n;
break
;
}
给个例子:
1
2
3
4
5
6
7
8
int
flag =
3
;
switch
(flag){
case
0
: System.out.println(
"The number is 0"
);
break
;
case
1
: System.out.println(
"The number is 1"
);
break
;
case
2
: System.out.println(
"The number is 2"
);
break
;
default
: System.out.println(
"you are right"
);
break
;
}
这是一个很标准的 switch 的用法, 有break, 还有 default ,在什么情况下跳出, 在什么情况下结束运行, 都很清楚的说明了, 但是朋友们在写这个代码的时候, 有可能会忘记加入break 或者 忘记 加入 default 这样的问题吧。
解决办法:
如果使用的是 IDEA 编译器, 可以加入语法提示, 一般默认都是开启的, 如果没开的话 步骤如下:
点击: setting - Editor - Inspections - Java - Control flow
把所有的勾选上去就完事了
如果使用的是 eclipse 编译器, 步骤如下:
Preferences-Java-Compiler-Errors/Warnings-Potential programming problems
直接使用switch的第二种用法
switch - > 用法:
使用 - > 方法更加简单, 就不用使用 break 命令, 保证只有一种路径会被执行!
用法如下:
1
2
3
4
5
6
7
8
int
flag =
3
;
switch
(flag){
case
0
-> System.out.println(
"The number is 0"
);
case
1
-> System.out.println(
"The number is 1"
);
case
2
-> System.out.println(
"The number is 2"
);
default
-> System.out.println(
"you are right"
);
}
去掉了 break 和 冒号 取代的使用 箭头 ->
不过瘾? 还有一种方式。 用来接受参数。
switch yield 用法:
看到 yield 的时候, 我就在想, 这一定要传值回来的函数。 用法 也很简单,
但是需要注意一点, 传值回去, 外部一定要定义个参数 用来接受这个值。
1
2
3
4
5
6
7
8
9
10
11
12
int
flag =
2
;
String ss;
ss =
switch
(flag){
case
0
->
"The number is 0"
;
case
1
->
"The number is 1"
;
case
2
-> {
String c =
"The number is 2"
;
yield c;
}
default
->
"you are right"
;
};
System.out.println(ss);
这里来说, switch 作为了 一条语句, 所以末尾要加冒号, 其他用法相同, 下次如果条件很多的话, 就不用使用 if else ,可以考虑使用 switch 中的yield 返回参数方式了!
石头剪刀布 测试代码:
学了switch的这些用法, 我想应该写个程序来试试吧, 所以就写了个石头剪刀布的小游戏
结果真的惨,,,,
代码如下: 可以学习参考
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import
java.util.HashMap;
import
java.util.Random;
import
java.util.Scanner;
public
class
test {
private
HashMap
new
HashMap
public
test() {
game.put(
1
,
"石头"
);
game.put(
2
,
"剪刀"
);
game.put(
3
,
"步"
);
}
public
void
randomGame() {
Scanner in =
new
Scanner(System.in);
System.out.println(
"这是一个石头剪刀布的游戏\n"
+
"我们学着使用 switch 语法 来完成这个小游戏程序! \n"
+
"Let me start!"
);
int
num =
3
;
boolean
flag =
true
;
int
win =
0
;
while
(flag) {
System.out.print(
"机器人已经准备好:\n请输入你的操作 如:石头\n输入:"
);
String myHand = in.next();
String robot = getRobot();
String s =
"你的出招为 "
+ myHand +
" 机器人的出招为 "
+ robot;
// 先判断相同情况下
if
(robot.equals(myHand)) {
System.out.println(s+
"平局!"
); }
// 判断不同情况下
else
if
( myHand.equals(
"剪刀"
))
{
switch
(robot){
case
"石头"
-> System.out.println(s +
" you lose"
);
case
"布"
-> {
System.out.println(s +
" you win"
);
win +=
1
;
}
}
}
else
if
(myHand.equals(
"石头"
)){
switch
(robot) {
case
"剪刀"
-> System.out.println(s +
" you lose"
);
case
"布"
-> {
System.out.println(s +
" you win"
);
win +=
1
;
}
}
}
else
if
(myHand.equals(
"布"
)){
switch
(robot) {
case
"剪刀"
-> System.out.println(s +
" you lose"
);
case
"石头"
-> {
System.out.println(s +
" you win"
);
win +=
1
;
}
}
}
else
{
System.out.println(
"你输入的"
+ myHand +
"是错误的"
);
}
num-=
1
;
if
(num ==
0
){flag =
false
;}
System.out.println(
"你还有"
+ num +
"次机会."
);
}
System.out.println(
"三局比赛中, 你一共获胜次数为 "
+ win);
}
public
String getRobot() {
// 得到机器人的出招
int
count = (
int
) (Math.random() *
10
/
3
);
if
(count >
0
){
return
game.get(count);
}
else
{
return
game.get(count +
1
);
}
}
public
static
void
main(String[] args) {
test me =
new
test();
me.randomGame();
}
}
return
game.get(count);
}
else
{
return
game.get(count +
1
);
}
}
public
static
void
main(String[] args) {
test me =
new
test();
me.randomGame();
}
}