freeCodeCamp/guide/chinese/java/control-flow/index.md

3.2 KiB
Raw Blame History

title localeTitle
Control Flow 控制流

控制流

控制流语句正是该术语的含义。它们是基于决策,循环和分支改变执行流程的语句,以便程序可以有条件地执行代码块。

首先Java具有以下用于流控制的构造

  • if

    if( <expression that results in a boolean> ){ 
        //code enters this block if the above expression is 'true' 
     } 
    
    
  • if...else

    if( <expression that results in a boolean> ){ 
        //execute this block if the expression is 'true' 
     } else{ 
        //execute this block if the expression is 'false' 
     } 
    
    
  • switch

当有多个值和要检查的案例时Switch是if...else构造的替代方案。

switch( <integer / String / Enum > ){ 
    case <int/String/Enum>: 
        <statements> 
        break; 
    case <int/String/Enum>: 
        <statements> 
        break; 
    default: 
        <statements> 
 } 

注:程序流程falls through下一case ,如果break语句丢失。例如,假设你对办公室里的每个人都说标准的“你好”,但对于坐在你旁边的女孩来说,你对你的老板说得好脾气。表示方式如下:

switch(person){ 
    case 'boss': 
        soundGrumpy(); 
        break; 
    case 'neighbour': 
        soundExtraNice(); 
        break; 
    case 'colleague': 
        soundNormal(); 
        break; 
    default: 
        soundNormal(); 
 } 
Note: The `default` case runs when none of the `case` matches. Remember that when a case has no `break` statement, it `falls through` to the next case and will continue to the subsequent `cases` till a `break` is encountered. Because of this, make sure that each case has a `break` statement. The `default` case does not require a `break` statement. 
  • nested statements

可以嵌套任何先前的控制流。这意味着您可以嵌套if if..elseswitch..case语句。也就是说,您可以将这些语句的任意组合放在另一个语句中,并且nesting的深度没有限制。

例如,让我们考虑以下场景:

  • 如果你的钱少于25美元你可以给自己一杯咖啡。
  • 如果你有超过25美元但不到60美元你会得到一个体面的饭菜。
  • 如果你有超过60美元但不到100美元你会得到一顿不错的美食和一杯葡萄酒。
  • 然而当你有超过100美元取决于你与谁在一起你要么去一个烛光晚餐与你的妻子或你去体育酒吧与你的朋友

表示这一点的方法之一是:

int cash = 150; 
 String company = "friends"; 
 
 if( cash < 25 ){ 
    getCoffee(); 
 } else if( cash < 60 ){ 
    getDecentMeal(); 
 } else if( cash < 100 ){ 
    getDecentMeal(); 
    getGlassOfWine(); 
 } else { 
    switch(company){ 
        case "wife": 
            candleLitDinner(); 
            break; 
        case "friends": 
            meetFriendsAtSportsBar(); 
            break; 
        default: 
            getDecentMeal(); 
    } 
 } 

在此示例中,将执行meetFriendsAtSportsBar()

:rocket: 运行代码