> java switch语句
而不是写很多if..else语句,您可以使用switch语句。
switch语句选择许多需要执行的代码块之一:>指向记住 开关表达式数量可以有一个或n个案例值。
案例值必须仅针对开关表达式类型。案例值必须是字面的或恒定的。它不允许变量。
案例值必须是唯一的。若有重复值,则在编译过程中会出现错误。
> java开关的表达式必须是字节,短,int,长(包装类型),枚举和字符串。
>>每个案例语句都可以有可选的中断语句。当控件到达中断语句时,它会在开关表达式后跳过控件。如果找不到断路语句,将执行下一种情况。
案例值可以有可选的默认标签。>
在java中,switch语句主要提供更详细的替代方案,避免嵌套或使用几个if-else语句与单个变量相关。>
在java中,switch语句也可以包含默认标签。默认标签只能在案例标签不匹配表达值的情况下执行。默认标签声明被认为是可选的,但在事故值或输入事件中可能非常有用。

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;
}
}
}
输出: “星期四”(第四天)
> 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 witchexample2 {
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");
}
}
}
输出:
20 30 不在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++;
}
}
}
输出:
0 1 2 3 4
注意:不要忘记增加条件中使用的变量,否则循环将永远不会结束!
>示例:
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!”。> ** 参考:**
以上是切换和循环的详细内容。请关注图灵教育的其他相关文章!