Java 决策制定

有两种类型的决策在Java中的语句,它们分别是:

  • if 语句

  • switch 语句

if 语句:

if语句由一个布尔表达式后跟一个或多个语句。

语法:

if语句的语法是:

if(Boolean_expression)
{
      //Statements will execute if the Boolean expression is true
}

如果布尔表达式的值为 true,那么代码里面的块if语句将被执行。如果不是第一套代码的if语句(后右大括号)结束后,将被执行。

例子:

public class Test {

   public static void main(String args[]){
      int x = 10;

      if( x < 20 ){
         System.out.print("This is if statement");
      }
   }
}

这将产生以下结果:

This is if statement

if...else 语句:

if 语句后面可以跟一个可选的 else 语句,语句执行时的布尔表达式为 false。

语法:

 if...else 的语法是:

if(Boolean_expression){
   //Executes when the Boolean expression is true
}else{
   //Executes when the Boolean expression is false
}

实例:

public class Test {

   public static void main(String args[]){
      int x = 30;

      if( x < 20 ){
         System.out.print("This is if statement");
      }else{
         System.out.print("This is else statement");
      }
   }
}

这将产生以下结果:

This is else statement

if...else if...else 语句:

if后面可以跟一个可选的 else if...else语句,这是一个使用单一的,测试各种条件下非常有用 if... else if语句。 

当使用 if , else if , else 语句时有几点要牢记。

  • if 可以有0个或没有 else 且它必须在else if 的之后。

  • if 可以有0个或多个 else if,但是它们必须在else之前。

  • 一旦 else if 成功, 余下else if 不会被测试执行。

语法:

if...else 的语法是:

if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
   //Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
   //Executes when the Boolean expression 3 is true
}else {
   //Executes when the none of the above condition is true.
}

例子:

public class Test {

   public static void main(String args[]){
      int x = 30;

      if( x == 10 ){
         System.out.print("Value of X is 10");
      }else if( x == 20 ){
         System.out.print("Value of X is 20");
      }else if( x == 30 ){
         System.out.print("Value of X is 30");
      }else{
         System.out.print("This is else statement");
      }
   }
}

这将产生以下结果:

Value of X is 30

嵌套 if...else 语句:

它始终是合法的嵌套if-else语句,这意味着你可以使用一个if或else if语句在另一个if或else if语句。

语法:

嵌套 if...else 的语法如下:

if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
   if(Boolean_expression 2){
      //Executes when the Boolean expression 2 is true
   }
}

可以嵌套 else if...else 在类似的方式,因为我们有嵌套的if语句。

实例:

public class Test {

   public static void main(String args[]){
      int x = 30;
      int y = 10;

      if( x == 30 ){
         if( y == 10 ){
             System.out.print("X = 30 and Y = 10");
          }
       }
    }
}

这将产生以下结果:

X = 30 and Y = 10

switch 语句:

switch 语句允许一个变量来对值的列表相等进行测试。每个值被称为一个例子,并且在选择测试该变量被检查的每种情况。

语法:

增强的 for循环的语法是:

switch(expression){
    case value :
       //Statements
       break; //optional
    case value :
       //Statements
       break; //optional
    //You can have any number of case statements.
    default : //Optional
       //Statements
}

以下规则适用于switch语句:

  • 在switch语句中使用的变量只能是一个字节,short,int和或char

  • 可以switch 有一个任何数量的case语句。每个案例后面进行比较的值和一个冒号。

  • 对于 case 的值必须是相同的数据类型作为开关变量,它必须是一个常量或文字。

  • 当被打开了变量等于的情况下,下列那 case 语句将执行,直到 break 语句为止。

  • 当达到一个break语句,switch 终止,并且控制流程跳转到下一行下面 switch语句。

  • 不是每一个 case 需要包含break。如果没有出现break,控制流将贯穿到后面的 case 直到 break 为止。

  • switch语句可以有一个可选默认 case ,它必须出现在 switch 的结束。缺省情况下,可用于执行任务时,没有case是true。没有break 是必须的,使用 default 。

例子:

public class Test {

   public static void main(String args[]){
      //char grade = args[0].charAt(0);
      char grade = 'C';

      switch(grade)
      {
         case 'A' :
            System.out.println("Excellent!"); 
            break;
         case 'B' :
         case 'C' :
            System.out.println("Well done");
            break;
         case 'D' :
            System.out.println("You passed");
         case 'F' :
            System.out.println("Better try again");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}

编译并运行上面使用各种命令行参数的程序。这将产生以下结果:

$ java Test
Well done
Your grade is a C
$
原文地址:https://www.cnblogs.com/QQ931697811/p/4421490.html