Java选择结构习题

学习  ·  2020-03-12

1、闰年的判断条件:

年份数能被4整除且不能被100整除,或者能被400整除。2、从键盘上输入4位数的年份,判断是否为闰年。
参考代码片段

if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)){
    System.out.println(year+“是闰年”);
}else{
     System.out.println(year+“不是闰年”);
}

2、判断水仙花数

需求说明:
从键盘录入一个整数
判断录入的整数是否是水仙花数。
水仙花数是一个三位的整数,其各位数字立方和等于该数本身。
例如:153=111+555+333

import java.util.*;
public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
     System.out.print("请输入一个三位的整数:");
         int num=input.nextInt();
         int i = num / 100;     /*分解出百位*/
         int j = num / 10 % 10; /*分解出十位*/
         int k = num % 10;      /*分解出个位*/
         if (i * 100 + j * 10 + k == i * i * i + j * j * j + k * k * k){
           System.out.println(num+"是水仙花数.");
        } else{
            System.out.println(num+"不是水仙花数.");
        }
}

3、通过键盘输入某年某月某日,计算并输出这一天是这一年的第几天。例如,2001年3月5日是这一年的第64天。

/*
输入某年某月某日,判断这一天是这一年的第几天。例如,2001年3月5日是这一年的第64天。
 */
package javaapplication33;
import java.util.Scanner;
public class JavaApplication33 {
    public static void main(String[] args) {
            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 sum=0;
            boolean leap=true;
            switch (month)/*先计算某月以前月份的总天数*/
            {
                case 1: sum = 0; break;
                case 2: sum = 31; break;
                case 3: sum = 59; break;
                case 4: sum = 90; break;
                case 5: sum = 120; break;
                case 6: sum = 151; break;
                case 7: sum = 181; break;
                case 8: sum = 212; break;
                case 9: sum = 243; break;
                case 10: sum = 273; break;
                case 11: sum = 304; break;
                case 12: sum = 334; break;
                default: System.out.println("data error"); 
            }
            sum = sum + day;/*再加上某天的天数*/
            /*判断是不是闰年*/
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
            {    leap = true;   }
            else
            {   leap = false;    }
            if (leap == true && month > 2)//如果是闰年且月份大于2,总天数应该加一天
            {   sum++;    } System.out.println(year+"年"+month+"月"+day+"日是这一年的第"+sum+"天.");
    }    
}

4、从键盘上输入一个年份值和一个月份值,输出该月的天数。(说明:一年有12个月,大月的天数是31,小月的天数是30。2月的天数比较特殊,遇到闰年是29天,否则为28天。例如,输入2011、3,则输出31天。)

import java.util.*;
public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    System.out.print("请输入年份:");
        int year = input.nextInt();
        System.out.print("请输入月份:");
        int month = input.nextInt();
        int numdays = 0;
        switch (month)
        {
           //当月份为1,3,5,7,8,10,12时,该月的天数为31
           case 1: 
           case 3:    
           case 5: 
           case 7:    
           case 8: 
           case 10: 
           case 12:    
                  numdays = 31;
           break;
           case 2:        //当月份为2时,首先需要判断输入的年份是否为闰年,如果是闰年,则为29天,否则为28天
                {
                    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
                    {
                        numdays = 29;
                    }
                    else
                    {
                        numdays = 28;
                    }
                    break;
                }
            case 4:        //当月份为4,6,9,11时,该月的天数为30
            case 6:
            case 9:
            case 11:
                   numdays = 30;
                break;
        }
        System.out.println(year+"年"+ month+"月有"+numdays+"天");
    }
 Java习题
评论
亦桉小站. All Rights Reserved.

赣ICP备19000739号-3