程序的流程控制-分支结构 if

1.分支结构:if语句

第一种格式:

/*
    if(条件表达式){
        语句体;
    }
    其它语句
*/
public class IfDemo1{
    public static void main(String[] args){
        int i = 10;
        if(i < 5){
            System.out.println("i > 5");
        }
        System.out.println("其它语句");
    }
}

第二种格式

/*
	if(条件表达式){
		语句体1;
	}else{
		语句体2;
	}
	其它语句
	
*/
public class IfDemo2{
	public static void main(String[] args){
		int i = 10;
		if(i < 5){
			System.out.println("i > 5");
		}else{
			System.out.println("i <= 5");
		}
		
		System.out.println("其它语句");
	}
}


第三种格式

 

/*
    if(条件表达式1){
        语句体1;
    }else if(条件表达式2){
        语句体2;
    }else if(条件表达式3){
        语句体3;
    }
    ...
    else{
        语句体n;
    }
*/
public class IfDemo3{
    public static void main(String[] args){
        
        int score = 101;
        if(score < 0){
            System.out.println("分数是负数");
        }else if(score < 60){
            System.out.println("不及格");
        }else if(score < 90){
            System.out.println("良好");
        }else if(score < 100){
            System.out.println("优秀");
        }else if(score == 100){
            System.out.println("满分");
        }else{
            System.out.println("非法分数");
        }
    }    
}

 2eg

//从键盘输入两个数,显示其中的最大值,要求使用if-else结构

import java.util.Scanner;//导入Scanner类

public class GetMax{
        public static void main(String[] args){
            Scanner s = new Scanner(System.in);
            System.out.print("请输入第一个数:");
            int a = s.nextInt();
        
            System.out.print("请输入第二个数:");
            int b = s.nextInt();
            if(a>b){
                System.out.println("最大值是:"+a);
            }else {
                System.out.println("最大值是:"+b);
            }
        }
    
}
/*
两种方式实现对两个整数变量的值进行互换
*/

public class ChangeNub{
        public static void main(String[] args){
            int a = 10;
            int b = 20;
            System.out.println("互换前 a =" + a + ",b="+b);
            a =  a + b;
            b =  a - b;
            a =  a - b;
      /* a = a^b;
        b = a^b;
        a = a^b
      */ System.out.println(
"互换后 a =" + a + ",b="+b); } }
/*
从键盘录入年龄,根据年龄给出提示:
35岁以下显示“青年”;
35-45岁显示“中年”;
46-75岁显示“老年”;
*/
import java.util.Scanner;

public class IfDemo{
    public static void main(String[] args){
        //创建扫描器对象
        Scanner s = new Scanner(System.in);
        
        //记录从键盘上录入的值
        System.out.print("请输入年龄:");
        int age = s.nextInt();//阻塞式方法
        
        //判断
        if(age < 0){
            System.out.println("非法年龄");
        }else if(age < 35){
            System.out.println("青年");
        }else if(age <= 45){
            System.out.println("中年");
        }else if(age <= 75){
            System.out.println("老年");
        }else if(age < 120){
            System.out.println("年龄大于75");
        }else{
            System.out.println("非法年龄");
        }
        
    }
}
/*
从键盘输入三个数,显示其中的最大值,要求使用if-else和三元运算符两种方式实现

*/
import java.util.Scanner;

public class IfDemo4{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.print("请输入第一个数:");
        int a = s.nextInt();
        System.out.print("请输入第二个数:");
        int b = s.nextInt();
        System.out.print("请输入第三个数:");
        int c = s.nextInt();
        /*
        if(a > b){
            if(a > c){
                System.out.println("最大值是:" + a);
            }else{
                System.out.println("最大值是:" + c);
            }
        }else{ //b >= a
            if(b > c){
                System.out.println("最大值是:" + b);
            }else{    //c>=b
                System.out.println("最大值是:" + c);
            }
        }
        */
        
        int res = (a > b)?a:b;
        int max = (res > c)?res:c;
        System.out.println("最大值是:" + max);
        
        // System.out.println("最大值是:" + ((a > b)?(a>c?a:c):(b>c?b:c)));
    }
}

 

posted on 2018-04-07 14:43  长空剑使  阅读(215)  评论(0编辑  收藏  举报