Java基础(极客)——05、Java if判断语句的用法

来源:互联网 发布:兆芯cpu运行windows 编辑:程序博客网 时间:2024/06/05 03:11
/**
 * 1、Java If语句概述和使用格式
 * 
 * 
 *  */

public class IfDemo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);//键盘输入,也就是在控制台输入
        System.out.println("输入分数:");
        int score = scanner.nextInt();
        if (score >= 60) {
            System.out.println(score + "及格");


        }



    }


}//class


/**
 * 2、Java If语句使用格式2-if else语句
 * 
 *
 */

public class IfDemo2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);//键盘输入,也就是在控制台输入
        System.out.println("输入第一个数");
        int a = scanner.nextInt();
        System.out.println("输入第二个数");
        int b = scanner.nextInt();
        if (a > b) {
            System.out.println(a + "是最大数");


        } else {
            System.out.println(b + "是最大数");


        }


    }


}//class



/**
 * 3、Java If语句使用格式3-if嵌套语句
 * 下面这些逻辑和swich case语句一样
 */

public class IfDemo3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);//键盘输入,也就是在控制台输入
        System.out.println("输入5分之数字:");
        int score = scanner.nextInt();


        if (score == 5) {
            System.out.println("优秀");


        } else if (score == 4) {
            System.out.println("良好");
        } else if (score == 3) {
            System.out.println("及格");
        } else if (score >= 0 && score <= 2) {
            System.out.println("不及格");
        } else {
            System.out.println("你输入的5分制有误");
        }


    }


}//class



/**
 * 4、【Java If语句算法示例1】键盘输入Java Android sql三科分数 找最高分
 * 
 * 
 */

public class IfDemo4 {
    public static void main(String[] args) {
        //找到最高分
        Scanner scanner = new Scanner(System.in);//键盘输入,也就是在控制台输入
        System.out.println("输入java的分数");
        int java = scanner.nextInt();
        System.out.println("输入android的分数");
        int android = scanner.nextInt();
        System.out.println("输入sql的分数");
        int sql = scanner.nextInt();


        if (java >= android && java >= sql) {
            System.out.println("最高分:" + java);
        } else if (android > sql) {
            System.out.println("最高分:" + android);


        } else {
            System.out.println("最高分:" + sql);
        }


    }
}//class


/**
 * 5、【Java If语句算法示例2】选择法找三科分数最高分(未完持续)
 * 
 *if(){}
 *else if(){}
 *和if(){}
 *if(){}
 *的区别
 *如果 if(){}成立则else if(){}不执行了
 *如果if(){}是否成立都会执行if(){}
 * 


 */

public class IfDemo5 {
    public static void main(String[] args) {
        //定义一个变量maxScore找到最高分:
        Scanner scanner = new Scanner(System.in);//键盘输入,也就是在控制台输入


        System.out.println("输入java分数:");
        int java = scanner.nextInt();
        System.out.println("输入android分数:");
        int android = scanner.nextInt();
        System.out.println("输入sql分数:");
        int sql = scanner.nextInt();


        //定义一个maxScore找到最高分
        int maxScore = java;


        if (maxScore <= android) {
            maxScore = android;


        }
        if (maxScore <= sql) {
            maxScore = sql;


        }
        System.out.println("最高分:" + maxScore);
    }
}//class




源码下载:
http://download.csdn.net/detail/zhaihaohao1/8741715
视频下载:
http://c38.yunpan.360.cn/my/index/#%2F%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91%2Fjava%2F


0 0
原创粉丝点击