> java switch语句
而不是编写许多if..else语句,您可以使用switch语句。
switch语句选择要执行的许多代码块之一:>
指向记住 可以有一个或n个案例值的开关表达式数量。
案例值必须仅针对开关表达式类型。案例值必须是字面的或恒定的。它不允许变量。
案例值必须是唯一的。如果具有重复值,则会呈现编译时错误。
> java开关表达式必须是字节,短,int,长(带有包装器类型),枚举和字符串。
>
>每个案例语句都可以具有可选的中断语句。当控件到达中断语句时,它会在开关表达式之后跳跃控件。如果找不到断路语句,它将执行下一个情况。
案例值可以具有可选的默认标签。>
在java中,switch语句主要提供了一种更详细的替代方案,该替代方案避免了与单个变量关联时的嵌套或几个if-else语句的使用。
>
在java中,switch语句还可以包含默认标签。默认标签只有在案例标签都不匹配表达式值的情况下才能执行。默认标签的声明被认为是可选的,但在意外值或输入的事件中可能很有用。
> andtax
switch(expression) { case x: // code block break; case y: // code block break; default: // code block}
登录后复制这就是它的工作方式:
>一次评估开关表达式。>将表达式的值与每种情况的值进行比较。
如果有匹配,则执行了相关的代码块。>
中断和默认关键字是可选的,将在本章后面进行描述。
下面的示例使用工作日编号来计算工作日名称:>
>示例
:1
public class main { public static void main(string[] args) { int day = 4; switch (day) { case 1: system.out.println("monday"); break; case 2: system.out.println("tuesday"); break; case 3: system.out.println("wednesday"); break; case 4: system.out.println("thursday"); break; case 5: system.out.println("friday"); break; case 6: system.out.println("saturday"); break; case 7: system.out.println("sunday"); break; } }}
登录后复制
输出: “星期四”(第4天)
> break关键字>
> java达到断路关键字时,它会突破开关块。>
这将停止执行更多代码和案例测试。
找到比赛并且完成工作时,是时候休息了。无需进行更多测试。*>休息时间可以节省大量执行时间,因为它“忽略”了开关块中所有代码的执行。*
>默认关键字
>默认关键字指定某些代码,如果没有案例匹配:>
> example
>
int day = 4;switch (day) { case 6: system.out.println("today is saturday"); break; case 7: system.out.println("today is sunday"); break; default: system.out.println("looking forward to the weekend");}
登录后复制
>输出
“期待周末”
示例:1
>
public class switchexample { public static void main(string[] args) { //declaring a variable for switch expression int number=20; //switch expression switch(number){ //case statements case 10: system.out.println("10"); break; case 20: system.out.println("20"); break; case 30: system.out.println("30"); break; //default case statement default:system.out.println("not in 10, 20 or 30"); } } }
登录后复制
输出:
20
>查找月示例:
//java program to demonstrate the example of switch statement //where we are printing month name for the given number public class switchmonthexample { public static void main(string[] args) { //specifying month number int month=7; string monthstring=""; //switch statement switch(month){ //case statements within the switch block case 1: monthstring="1 - january"; break; case 2: monthstring="2 - february"; break; case 3: monthstring="3 - march"; break; case 4: monthstring="4 - april"; break; case 5: monthstring="5 - may"; break; case 6: monthstring="6 - june"; break; case 7: monthstring="7 - july"; break; case 8: monthstring="8 - august"; break; case 9: monthstring="9 - september"; break; case 10: monthstring="10 - october"; break; case 11: monthstring="11 - november"; break; case 12: monthstring="12 - december"; break; default:system.out.println("invalid month!"); } //printing month of the given number system.out.println(monthstring); } }
登录后复制
输出:
7- 7月
>检查元音或辅音的程序:
>如果字符为a,e,i,o或u,则是元音辅音。它不是对大小写的。>
public class switchvowelexample { public static void main(string[] args) { char ch='o'; switch(ch) { case 'a': system.out.println("vowel"); break; case 'e': system.out.println("vowel"); break; case 'i': system.out.println("vowel"); break; case 'o': system.out.println("vowel"); break; case 'u': system.out.println("vowel"); break; case 'a': system.out.println("vowel"); break; case 'e': system.out.println("vowel"); break; case 'i': system.out.println("vowel"); break; case 'o': system.out.println("vowel"); break; case 'u': system.out.println("vowel"); break; default: system.out.println("consonant"); } } }
登录后复制
>输出:
元音java switch语句是秋天的java switch语句是秋季的。这意味着如果不存在断路语句,则在第一次匹配后执行所有语句。
>
>示例:
//java switch example where we are omitting the //break statement public class switchexample2 { public static void main(string[] args) { int number=20; //switch expression with int value switch(number){ //switch cases without break statements case 10: system.out.println("10"); case 20: system.out.println("20"); case 30: system.out.println("30"); default:system.out.println("not in 10, 20 or 30"); } } }
登录后复制
输出:
2030不在10、20或30
中
> java switch语句带有字符串(tbd)>>
>示例:
switchstringexample.java //java program to demonstrate the use of java switch //statement with string public class switchstringexample { public static void main(string[] args) { //declaring string variable string levelstring="expert"; int level=0; //using string in switch expression switch(levelstring){ //using string literal in switch case case "beginner": level=1; break; case "intermediate": level=2; break; case "expert": level=3; break; default: level=0; break; } system.out.println("your level is: "+level); } }
登录后复制
输出: 您的级别是:3
> java嵌套开关语句
>>我们可以在java中的其他switch语句中使用switch语句。它被称为嵌套开关语句。
>示例:
nestedswitchexample.java //java program to demonstrate the use of java nested switch public class nestedswitchexample { public static void main(string args[]) { //c - cse, e - ece, m - mechanical char branch = 'c'; int collegeyear = 4; switch( collegeyear ) { case 1: system.out.println("english, maths, science"); break; case 2: switch( branch ) { case 'c': system.out.println("operating system, java, data structure"); break; case 'e': system.out.println("micro processors, logic switching theory"); break; case 'm': system.out.println("drawing, manufacturing machines"); break; } break; case 3: switch( branch ) { case 'c': system.out.println("computer organization, multimedia"); break; case 'e': system.out.println("fundamentals of logic design, microelectronics"); break; case 'm': system.out.println("internal combustion engines, mechanical vibration"); break; } break; case 4: switch( branch ) { case 'c': system.out.println("data communication and networks, multimedia"); break; case 'e': system.out.println("embedded system, image processing"); break; case 'm': system.out.println("production technology, thermal engineering"); break; } break; } } }
登录后复制 输出:
数据通信和网络,多媒体
java enum in switch语句
> java允许我们在switch语句中使用enum。 java enum是代表常数组的类。 (不变的,例如最终变量)。我们使用关键字枚举,并将常数放在由逗号隔开的卷发括号中。
>示例:
javaswitchenumexample.java //java program to demonstrate the use of enum //in switch statement public class javaswitchenumexample { public enum day { sun, mon, tue, wed, thu, fri, sat } public static void main(string args[]) { day[] daynow = day.values(); for (day now : daynow) { switch (now) { case sun: system.out.println("sunday"); break; case mon: system.out.println("monday"); break; case tue: system.out.println("tuesday"); break; case wed: system.out.println("wednesday"); break; case thu: system.out.println("thursday"); break; case fri: system.out.println("friday"); break; case sat: system.out.println("saturday"); break; } } } }
登录后复制>输出:
>星期日
星期一
twesday
星期三星期四星期五星期六
关于java switch语句
>(tbd)
的重要点
>关于java switch语句的主要重要功能之一是它通过行为跌落。这意味着,如果案例标签不包含断点语句,则执行将直接传递给下一个案例标签。开关语句还可以包括一个默认标签,如果案例标签都不匹配表达式的值,则执行该标签。默认标签是可选的,但对于处理意外或未指定的值很有用。
>
开关语句只能匹配精确的值,并且无法检查值的范围。这意味着,如果您需要检查一系列值,则需要使用多个案例标签或求助于其他控制流语句。
开关语句只能用于检查表达式和案例标签之间的平等。他们无法执行更复杂的检查,例如检查条件或使用比较操作员。
开关语句中的表达式必须评估为原始数据类型(int,char或enum)或字符串(由于java 7)。此限制限制了可以与开关语句一起使用的表达式类型,与其他控制流语言相比,在某些情况下它们的灵活性较低。
>值得注意的是,从java 12开始,switch语句已得到增强以支持开关表达式,这使switch语句可以用作返回值的表达式。此功能提供了更大的灵活性,并可以在某些情况下导致更简洁,更可读的代码。
>总体而言,java中的switch语句是基于表达式值控制程序流的强大工具,提供了一种清晰有效的方法来处理多个分支方案。开关语句有时会导致代码重复,尤其是当多个情况块执行类似的操作时。这可能会使代码更难维护和调试。>>自java 7以来可以与字符串对象一起使用switch语句,但不能直接与其他对象类型一起使用。在需要比较复杂的对象的情况下,此限制可以使开关语句降低。
参考
:
>
参考
:
>
java while loop
>
loopshttps://www.javatpoint.com/java-switch
只要达到指定条件,
循环就可以执行代码块。循环很方便,因为它们节省了时间,减少错误并使代码更可读。>java时循环只要指定条件是正确的,while循环循环通过代码块:https://www.w3schools.com/java/java_switch.asp
> andtax
while (condition) { // code block to be executed}
登录后复制
在下面的示例中,循环中的代码将一遍又一遍地运行,只要变量(i)小于5:
>> example
>
public class main { public static void main(string[] args) { int i = 0; while (i < 5) { system.out.println(i); i++; } }}
登录后复制
输出:
01234
注意:不要忘记增加条件中使用的变量,否则循环将永远不会结束!
>示例:
public class main { public static void main(string[] args) { int countdown = 3; while (countdown > 0) { system.out.println(countdown); countdown--; } system.out.println("happy new year!!"); }}
登录后复制
输出:
3
2
1
新年快乐!!
>示例:
public class Main { public static void main(String[] args) { int dice = 1; while (dice <= 6) { if (dice < 6) { System.out.println("No Yatzy."); } else { System.out.println("Yatzy!"); } dice = dice + 1; } }}
登录后复制
输出:
没有yatzy。没有yatzy。没有yatzy。
没有yatzy。
没有yatzy。
yatzy! 如果循环通过1到5的值,则打印为“ no yatzy”。每当它通过值6时,它都会打印“ yatzy!”。> **
参考:**
以上就是切换,循环的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/3051974.html