• 程序流程


    if语句和逻辑运算符的使用

    package computerArea;
    import java.util.Scanner;
    public class ComputerArea {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个数");
            int num = sc.nextInt();
            if(num % 5 == 0 && num % 6 == 0){
                System.out.println(num+"能被5和6整除。");
            }
            else if(num % 5 == 0 || num % 6 == 0){
                System.out.println(num+"能被5或6整除");
            }
            else if(num % 5 == 0 ^ num % 6 == 0){
                System.out.println(num+"能只被5或者只被6整除");
            }
            else
                System.out.println(num+"不能被5和6中任意一个数整除");
        }
    }
          

     判断闰年

    package computerArea;
    import java.util.Scanner;
    public class ComputerArea {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入年份:");
            int year = sc.nextInt();
            if((year % 4 == 0 && year % 100 == 0) || year % 400 == 0){
                System.out.println(year+"年是闰年");
            }
            else{
                System.out.println(year+"年不是闰年");
            }
        }
    }
          

     if和String

    package computerArea;
    import java.util.Scanner;
    public class ComputerArea {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您的成绩");
            double score = sc.nextDouble();
            String grade = "";
            if(score > 100 || score < 0){
                System.out.println("您输入的成绩不在合法范围");
                System.exit(0);
            }else if(score >= 90){
                grade = "优秀";
            }else if(score >= 80){
                grade = "良好";
            }else if(score >= 70){
                grade = "中等";
            }else if(score >= 60){
                grade = "及格";
            }else
                grade = "不及格";
            System.out.println("你的成绩为"+grade);
        }
    }
          
  • 相关阅读:
    C#之集合常用扩展方法与Linq
    PHP核心之MVC设计模式
    Javascript高级之变量
    Javascript高级之console调试
    Javascript高级之概述
    MySQL数据库之PDO扩展
    MySQL数据库之MySQL扩展
    MySQL数据库之数据库备份与还原
    MySQL数据库之预处理
    MySQL数据库之函数
  • 原文地址:https://www.cnblogs.com/rain-1/p/4859991.html
Copyright © 2020-2023  润新知