登录
首页 >  文章 >  java教程

切换,循环

来源:dev.to

时间:2025-01-31 23:57:48 357浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《切换,循环》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

> java switch语句

而不是编写许多if..else语句,您可以使用switch语句。

switch语句选择要执行的许多代码块之一:>

指向记住 可以有一个或n个案例值的开关表达式数量。

案例值必须仅针对开关表达式类型。案例值必须是字面的或恒定的。它不允许变量。

案例值必须是唯一的。如果具有重复值,则会呈现编译时错误。

> java开关表达式必须是字节,短,int,长(带有包装器类型),枚举和字符串。

>

>每个案例语句都可以具有可选的中断语句。当控件到达中断语句时,它会在开关表达式之后跳跃控件。如果找不到断路语句,它将执行下一个情况。

案例值可以具有可选的默认标签。>

在java中,switch语句主要提供了一种更详细的替代方案,该替代方案避免了与单个变量关联时的嵌套或几个if-else语句的使用。

>

在java中,switch语句还可以包含默认标签。默认标签只有在案例标签都不匹配表达式值的情况下才能执行。默认标签的声明被认为是可选的,但在意外值或输入的事件中可能很有用。

image description

> 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");  
        }  
    }  
    }  

输出:

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!”。> **
参考:**


今天关于《切换,循环》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>