JAVA Series -> determination statement if, if ... else, if..else if ... else, if replacement ternary operator

Judgment statement

1-if statement determines

format:

if(关系表达式){
    语句体;

Implementation process:
first determine the relationship between the expression to see the result is true or false, if the statement is true on the implementation of the body, if the statement is false does not execute body.

Flowchart:
Here Insert Picture Description
Example:

public static void main(String[] args){ 
    System.out.println("Begin");
     // 定义两个变量
    int a = 10; int b = 20;
     //变量使用if判断
    if (a == b){
        System.out.println("a=b"); 
     }
     int c = 10; 
     if(a == c){ 
         System.out.println("a=c");
      }
        System.out.println("End"); }

operation result:
Here Insert Picture Description

Judge sentences 2-if ... else

format:

if(关系表达式) { 
  语句体1; 
  }else { 
  语句体2;
 }

Implementation process:
first determine the relationship between the expression to see the result is true or false, if the statement is true on the implementation of 1, if a false statement on the implementation of the body.

flow chart:
Here Insert Picture Description

For example:

public static void main(String[] args){
   // 判断给定的数据是奇数还是偶数
   // 定义变量
    int a = 1;
    if(a % 2 == 0) {    //取余运算  之前提过
      System.out.println("a是偶数"); 
     } else{ 
        System.out.println("a是奇数"); 
    }
        System.out.println("结束");
     }

operation result:
Here Insert Picture Description

Judge sentences 3-if ... else if ... else

format:

if (判断条件1) { 
    执行语句1; 
  } else if (判断条件2) { 
    执行语句2; }
... 
  }else if (判断条件n) {
     执行语句n; 
   } else { 
     执行语句n+1;
}

Implementation process:
first determine the relationship between the expression 1 to see the result is true or false, if the statement is true on the implementation of 1, if it is false to continue to determine the relationship between the expression 2 to see the result is true or false, if it is true the statement is executed 2, if it is false to continue to determine the relationship between the expression ... see the result is true or false, ..., if there is no relationship between the expression is true, then the statement is executed body n + 1.

flow chart:
Here Insert Picture Description

Example:
public static void main (String [] args) {
// x and y satisfy the relation as follows:
// x> = y = 2x +. 1. 3;
// -. 1 <= x <= y 2x. 3;
// x <= - y = 2x. 1 -. 1;
// the given values of x, y and the calculated output value.
// define the variable
int X =. 5;
int Y;
IF (X> =. 3) {
Y = 2 * X +. 1;
} the else IF (X> X = -1 && <. 3) {
Y = 2 * X;} {the else
y = 2 * X -. 1;
}
System.out.println ( "value of y is:" + y);
}

Exercises:
Specifies the test scores to determine student grades
90-100 Excellent
80-89 Good
70-79 Good
60-69 pass
60 or less failed.

  public static void main(String[] args) {
        int score = 100;
        if (score < 0 || score > 100) {
            System.out.println("你的成绩是错误的");
        } else if (score >= 90 && score <= 100) {
            System.out.println("你的成绩属于优秀");
        } else if (score >= 80 && score < 90) {
            System.out.println("你的成绩属于好");
        } else if (score >= 70 && score < 80) {
            System.out.println("你的成绩属于良");
        } else if (score >= 60 && score < 70) {
            System.out.println("你的成绩属于及格");
        } else {
            System.out.println("你的成绩属于不及格");
        }
    }

if statement and the ternary operator exchange

 public static void main(String[] args) {
        int a = 10;
        int b = 20;
        //定义变量,保存a和b的较大值
        int c;
        if (a > b) {
            c = a;
        } else {
            c = b;
        }
        //可以上述功能改写为三元运算符形式
         c = a > b ? a:b;
        System.out.println(c);
    }
Published 37 original articles · won praise 24 · views 664

Guess you like

Origin blog.csdn.net/qq_16397653/article/details/103635940