JAVA笔记二十六:练习题(二)

一、面向过程

public class Tools {
    //没有参数,没有返回值
    public void m1() {
        //方法内容 控制台上输出HelloWorld
        System.out.println("HelloWorld");
    }

    //没有参数,没有返回值
    public void m2() {
        //方法内容  比较两个整数大小输出较大的值
        int x = 10;
        int y = 20;
        if (x > y) {
            System.out.println(x);
        } else {
            System.out.println(y);
        }
    }

    //有参数,没有返回值
    public void m3(int x, int y) {
        if (x > y) {
            System.out.println(x);
        } else {
            System.out.println(y);
        }
    }

    //有参数 有返回值
    public int m4(int x, int y) {
        if (x > y) {
            return x;
        } else {
            return y;
        }

    }

}

一、面向对象

public class TextT {

    public static void main(String[] args) {
        //使用Tools类 创建一个对象 t
        Tools t = new Tools();
        //调用方法m1 在控制台上输出HelloWorld
        t.m1();
        //调用方法m2 在控制台上输出最大值
        t.m2();
        //调用方法m3 在控制台上输出传入参数的最大值
        t.m3(23, 48);
        int a = 20;
        int b = 40;
        int c = 10;
        //调用方法 m4 返回最大值给max
        int max = t.m4(a, b);
        max = t.m4(max, c);
        System.out.println("最大值为" + max);

    }

}

二、面向过程

public class work2 {
    //    public int num(int a,int b){
//        // 在类中编写一个方法,返回n至m所有数字的和
//        int sum = 0;
//        if(a<b){
//            for(int i=a;i<=b;i++){
//                sum+=i;
//            }
//        }else {
//            for(int i=a;i<=b;i++){
//                sum+=i;
//            }
//        }
//        return sum;
//    }
    public int num(int year, int month, int day) {

        int[] leapYear = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};//闰年
        int[] commonYear = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};//平年

        int sumDays = 0;
        if (month > 12 || month < 1) {
            System.out.println("请输入正确的天数!");
        } else {
            //闰年
            boolean b = month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12;
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                //天数为31天的月份
                if (b) {
                    //判断天数是否在31天之内
                    if (day < 1 || day > 31) {
                        System.out.println("请输入正确的天数!");
                    } else {
                        sumDays = leapYear[month - 1] + day;
                    }
                } else if (month == 2) {
                    //判断天数是否在29天之内
                    if (day < 1 || day > 29) {
                        System.out.println("请输入正确的天数");
                    } else {
                        sumDays = leapYear[month - 1] + day;
                    }
                } else {
                    if (day < 1 || day > 30) {
                        System.out.println("请输入正确的天数");
                    } else {
                        sumDays = leapYear[month - 1] + day;
                    }
                }
            } else {
                //平年
                if (b) {
                    //判断天数是否在31天之内
                    if (day < 1 || day > 31) {
                        System.out.println("请输入正确的天数!");
                    } else {
                        sumDays = commonYear[month - 1] + day;
                    }
                } else if (month == 2) {
                    //判断天数是否在28天之内
                    if (day < 1 || day > 28) {
                        System.out.println("请输入正确的天数!");
                    } else {
                        sumDays = commonYear[month - 1] + day;
                    }
                } else {//为4、6、9、11月中的一月
                    //判断天数是否在30天之内
                    if (day < 1 || day > 30) {
                        System.out.println("请输入正确的天数!");
                    } else {
                        sumDays = commonYear[month - 1] + day;
                    }
                }
            }
        }
        return sumDays;
    }
}

二、面向对象

public class work2_1 {
    public static void main(String[] args) {
//        work2 t = new work2();
//        Scanner scan = new Scanner(System.in);
//        Scanner sca = new Scanner(System.in);
//        int a = scan.nextInt();
//        int b = sca.nextInt();
//        int num2 = t.num(a,b);
//        System.out.println(num2);


        work2 t = new work2();
        Scanner input=new Scanner(System.in);
        System.out.print("请输入您要查询的年份:");
        int year=input.nextInt();
        System.out.print("请输入您要查询的月份:");
        int month=input.nextInt();
        System.out.print("请输入您要查询的天数:");
        int day=input.nextInt();
        int num2 = t.num(year,month,day);
        System.out.println(num2);
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五毒幽泉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值