
JShell is based on the REPL (Read-Evaluate-Print-Loop) introduced in Java 9. This tool can be used to execute simple statements, evaluate it, and prints the result.
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.
<strong>jshell> int i = 10;
i ==> 10
jshell> switch(i) {
...> case 1 : System.out.println("1");
...> case 10 : System.out.println("10");
...> default : System.out.println("default");
...> }
10
default
jshell> int i = 1;
i ==> 1
jshell> switch(i) {
...> case 1 : System.out.println("1");
...> case 10 : System.out.println("10");
...> default : System.out.println("default");
...> }
1
10
default</strong>在下面的代码片段中,我们可以在JShell中定义一个带有break的switch语句。
<strong>jshell> switch(i) {
...> case 1 : System.out.println("1"); break;
...> case 10 : System.out.println("10"); break;
...> default : System.out.println("default"); break;
...> }
1</strong>以上就是在Java 9的JShell中如何定义switch语句?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号