JAVA基础100道练习题——输入年份求判断是平年还是闰年——求1~100之间的数字有多少个9

<1>题目介绍1

编写程序,让用户输入一个年份,判断是平年还是闰年,输出并打印

<2>思路分析

判断用户输入的是否为整千年,如果是整千年,该年份膜上400为0才是闰年。其他年份只要膜上4为0就是闰年

<3>代码实现

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            int a = scan.nextInt();
            if((a%4==0&&a%100!=0)||a%400==0){
                System.out.println(a+"是闰年");
            }
            else{
                System.out.println(a+"是平年");
            }
        }
    }

<4>结果展示

<1>题目介绍2

编写程序,判断1~100之间的数字中有多少的9

<2>思路分析

9、19、29......这些数字个位数上都有9,为了计算他们有多少个,这里可以对他们取模;

90、91、92......这些数字十位上面都有9,为了计算他们有多少个,这里可以让他们除以10.

<3>代码实现

public static void main(String[] args) {
        int i = 1;
        int count = 0;
        for(i=1;i<=100;i++){
            if(i%10==9) {
                count++;
            }if(i/10==9){
                count++;
            }
        }
        System.out.println("1~100的数字中一共有"+count+"个9");
}

<4>结果展示

猜你喜欢

转载自blog.csdn.net/yahid/article/details/123404045