选择结构

选择结构

Java中主要通过if语句来实现选择分支流程,if语句的格式有常用的三种:
单if语句:执行某些步骤,或者不执行它们。
标准的if-else 语句:在2套步骤方案中选择一种执行。
扩展的if-else语句:在3套或更多步骤方案中选择一种执行。

单if语句
格式:
if(布尔表达式){
语句体
}

public class Demo2If{
      public  static void  main(String[] args){
       int  age = 20;
       if(age>=18){  //符合条件 执行
       Ststem.out.println("进入。。。。。。"); 

        }
   }

}

标准的if-else 语句

格式:
if(布尔表达式){
语句体A
}else{
语句体B
}

执行流程:
1.首先判断小括号之内的条件是true 还是 false
2.如果是true,那么执行if 大括号内的语句体A,else内的内容将不再执行
3.如果是false ,那么执行else 大括号内的语句体B,而if 的内容将不再执行。

//标准的 if-else语句

import java.util.Scanner
public class Demo03IfElse{
public static void main(String[]  args){
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入一个整数");
    int num = Sc.nextInt();
    if (num % 2 == 0){
    System.out.println("偶数");
       }else{
       System.out.println("奇数");
       }
    
   }
}

扩展的if-else 语句

格式:
if(条件判断1){
语句体1
}else if(条件判断2){
语句体2
}

else if (条件判断N){
语句体N
}else{

语句体N+1
}

import java.util.Scanner

public class Demo04IfElseExt{
    public  static void main(String[] args){
     scanner sc = new scanner(System.in);
     System.out.println("请输入考试成绩:");
     int score = sc.nextInt();
     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 if (Score >=0 && score <60){
          System.out.println("不及格")}else{
          System.out.println("错误情况");
          }
               
     

}


}

求两者最大值

import java.util.Scanner
public class Demo05MaxTwo{

public static void main(Steing[]args ){
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数字")int a = sc.nextInt();
System.out.println("请输入第二个数字")int b = sc.nextInt();
int max;
if(a>b){
max = a;
}else{
max = b;
}
System.out.println("最大值 :"+ max)}


}

求出三个数字的最大值

package score;
import java.util.Scanner;
public class Demo06MaxThree {
	 public static void main(String[] args){
	     Scanner sc = new Scanner(System.in);
	     System.out.println("请输入第一个数字");
	     int a = sc.nextInt();
	     System.out.println("请输入第二个数字");
	     int b = sc.nextInt();
	     System.out.println("请输入第三个数字");
	     int c = sc.nextInt();
	     int temp;
	     if(a > b){
	    temp = a;
	     }else{
	    temp = b;
	}
	     int Max;
	if (temp > c){
	 Max = temp;
	}else{
	 Max = c;
	}
   System.out.println("最大值 :"+ Max);

	}


	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值