a switch statement can test multiple conditions just like an else clause and handles the default possibility. the default clause can be executed when none of the cases match, and a break statement can be used to break out of switch after a successful match.
in the below code snippet, we can define the switch statement in jshell.
snippet-1jshell> int i = 10;i ==> 10jshell> switch(i) { ...> case 1 : system.out.println("1"); ...> case 10 : system.out.println("10"); ...> default : system.out.println("default"); ...> }10defaultjshell> int i = 1;i ==> 1jshell> switch(i) { ...> case 1 : system.out.println("1"); ...> case 10 : system.out.println("10"); ...> default : system.out.println("default"); ...> }110default
在下面的代码片段中,我们可以在jshell中定义一个带有break的switch语句。
snippet-2jshell> switch(i) { ...> case 1 : system.out.println("1"); break; ...> case 10 : system.out.println("10"); break; ...> default : system.out.println("default"); break; ...> }1
以上就是在java 9的jshell中如何定义switch语句?的详细内容。