从控制台输入月份,输出本月有多少天。

import java.util.Scanner;

/**
 * @author 蓝色以太 输入某年某月某日,判断这一天是这一年的第几天?
 */
public class CalculateDay {

    public static void main(String[] args) {
        int y, m, d, F = 28, A = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("请正确输入年份:");
        y = sc.nextInt();
        System.out.println("请正确输入月份:");
        m = sc.nextInt();
        System.out.println("请正确输入日期:");
        d = sc.nextInt();
        if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
            F = 29;
            A = 1;
        }
        switch (m) {
        case 1: {
            if (d < 1 || d > 31) {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        case 2: {
            if (d >= 1 && d <= 29 && A == 1) {
                d = d + 31;
            } else if (d >= 1 && d <= 28) {
                d = d + 31;
            } else {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        case 3: {
            if (d >= 1 && d <= 31) {
                d = d + 31 + F;
            } else {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        case 4: {
            if (d >= 1 && d <= 30) {
                d = d + 31 + F + 31;
            } else {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        case 5: {
            if (d >= 1 && d <= 31) {
                d = d + 31 + F + 31 + 30;
            } else {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        case 6: {
            if (d >= 1 && d <= 30) {
                d = d + 31 + F + 31 + 30 + 31;
            } else {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        case 7: {
            if (d >= 1 && d <= 31) {
                d = d + 31 + F + 31 + 30 + 31 + 30;
            } else {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        case 8: {
            if (d >= 1 && d <= 31) {
                d = d + 31 + F + 31 + 30 + 31 + 30 + 31;
            } else {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        case 9: {
            if (d >= 1 && d <= 30) {
                d = d + 31 + F + 31 + 30 + 31 + 30 + 31 + 31;
            } else {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        case 10: {
            if (d >= 1 && d <= 31) {
                d = d + 31 + F + 31 + 30 + 31 + 30 + 31 + 31 + 30;
            } else {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        case 11: {
            if (d >= 1 && d <= 30) {
                d = d + 31 + F + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
            } else {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        case 12: {
            if (d >= 1 && d <= 31) {
                d = d + 31 + F + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
            } else {
                System.out.println("输入的日期有误!");
            }
            break;
        }
        default:
            System.out.println("月份有误,请重新输入");
        }
        System.out.println("这一天是这一年的第" + d + "天");
    }

}
时间: 2024-08-05 01:39:22

从控制台输入月份,输出本月有多少天。的相关文章

python实现输入月份查询本月天数

功能要求: 1. 输入年.月,输出本月有多少天.合理选择分支语句完成设计任务 输入样例:2014   2 输出样例:本月29天 2. 判断输入是否为空,如果为空,报错Error 源代码如下: 结果如下: 原文地址:https://www.cnblogs.com/ChairmanYSL/p/9548705.html

Java 控制台输入数字 输出乘法表(代码练习)

最近,回忆了一些刚学习Java时经常练习的一些小练习题.感觉还是蛮有趣的,在回顾时想起好多学习时的经历和坎坷,一道小小的练习题要研究半天,珍重过往,直面未来.下面贡献代码,Java 控制台输入数字 输出乘法表(代码练习).希望能给一些初学者一点点的启发.注释写的比较啰嗦,嘻嘻~ 因为博客园有要求"少于150字的随笔不允许发布到首页候选区",所以,凑几行字啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 //作者:我 //功能:九九乘法表 //时间:

【Python基础】python控制台输入、输出

python控制台输入.输出 1.输出 简单输出 print("我是简单的字符串输出") 控制台运行结果 我是简单的字符串输出 格式化输出 age=18 print("我今年%d岁"%age) age+=1 print("我今年%d岁"%age) age+=1 print("我今年%d岁"%age) 控制台打印结果: 我今年18岁 我今年19岁 我今年20岁 常用格式化符号: 格式符号 转换 %c 字符 %s 等同于str()

C#之控制台输入和输出

控制台输出 C# 控制台程序一般使用 .NET Framework Console 类提供的输入/输出服务.Console.WriteLine("Hello World!"); 语句使用 WriteLine 方法.它在命令行窗口中显示其字符串参数并换行.其他 Console 方法用于不同的输入和输出操作.Console 类是 System 命名空间的成员.如果程序开头没有包含using System; 语句,则必须指定System 类,如下所示:System.Console.Write

第二章 控制台输入与输出

2.1 控制台输出 printf("i am QLEO \n"); printf("我是谁谁谁 \n"); 2.1.1 输出整数 printf("I am %d \n",33); 2.1.2 用变量表示整数 int age = 33 printf("I am %d \n",age); // 同一行打印两个整数 int a = 10; int b = 50; printf("a: %d,b: %d \n",a

c# 控制台输入和输出

原文地址:https://www.cnblogs.com/fanweisheng/p/11440971.html

将控制台输入的每一行字符串,输出至txt文件当中

/** *    需求:将控制台输入的每一行字符串,输出至txt文件当中. /** * 需求:将控制台输入的每一行字符串,输出至txt文件当中. * 思路: * 1.首先想到BufferReader高级流读取一行字符串readLine方法. * 2.但是前提依赖于字符转换流ISR和低级节点流,这里是从控制台输入,节点流自然是System.in.如果是从文件输入,则节点流换成FIS即可 * 3.这样就能建立一条输入流了.BufferedReader(new InputStreamReader(Sy

C语言的字符串转为OC中的字符串,控制台输入用户名和密码,判断输入的用户名是否是@“Frank”, 密码 是否是 @“lanou”, 如果用户名和密码都正确,则输出登录成功, 否则输出登录失败.

char name[20] = "";存储用户名 char code[20] = "";存储密码 printf("请输入用户名与密码"); scanf("%s%s", name, code); NSString *str1 = [NSString stringWithFormat:@"%s", name ]; NSString *str2 =[NSString stringWithFormat:@"

JS 输入年份,再输入月份,弹出这个月有多少天?

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> </html> <script type="text/javascript"> var nian = Number(prompt("请输入年份&