选择结构包括以下三种
一.if语句
二.swich语句
三.条件运算符
if语句:是选择语句结构的基本语句,根据判断条件执行代码块,包括简单if,if···else if····else if·····else等三种 下面进行简单的运用
if语句:
package com.jsu.jie2;
import java.util.Scanner;
public class four {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int aInt= in.nextInt();
if(aInt>0){
System.out.println("输入的数字"+"为正数");
}
if(aInt==0){
System.out.println(aInt+"输入的数字为");
}
if(aInt<0){
System.out.println(aInt+"小于0!");
}
}
}
if····else语句
package com.jsu.jie2;
import java.util.Scanner;
public class four {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("请输入学生成绩以判断是否合格");
int x= in.nextInt();
if (x>60)
{
System.out.println("恭喜你的成绩合格");
}
else {
System.out.println("你的成绩不合格");
}
}
}
if····else if····else语句
实际应用中,有些场景使用if····else语句就能解决,但是在多条件判断的复杂场景时,if····else的能力有些不足。所以if····else if····else语句有了用武之地。
public class Test {
public static void four(String[] args) {
Scanner in=new Scanner(System.in);
int score=in.nextInt();
if( score >= 90 ){
System.out.print("优秀!");
}else if( score >=80 ){
System.out.print("良好");
}else if( score >=60 ){
System.out.print("及格");
}else{
System.out.print("不及格");
}
}
}