输入一个人的出生日期(包括年月日),将该生日中的年、月、日三行输出;输出时如果月份或天数为1位数,需要在1位数前面补0。

出生日期输入输出

http://www.nowcoder.com/questionTerminal/4a4a9dd1edb6453ba4a0432319200743

思考一下这道题

如果输入的长度不是8位数呢,我输6位类似与202092你们按照Substring(0,4),(4,6),(6,8)这样的代码确定不会报错吗???
肯定会错误啊

来看一下:这是系统中通过的代码

代码块
package com.yunze.niuketest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
          String s=in.readLine();
          String year=s.substring(0,4);
          String month=s.substring(4,6);
          String date=s.substring(6,8);
          System.out.println("year="+year);
          System.out.println("month="+month);
          System.out.println("date="+date);
    }
}

上述代码输入202092执行之后
图片说明
很显然出错了啊
这样系统怎么判定对的呢

我用最基础的写了一个

供大家参考,代码有压缩的空间,你们自行优化

代码块
package com.yunze.niuketest;

import java.util.Scanner;

public class NiuKE {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.nextLine();
        if (a.length() <= 8) {
            if (a.length() == 8) {
                String b = a.substring(0, 4);
                String c = a.substring(4, 6);
                String d = a.substring(6, 8);
                System.out.println("year=" + b);
                System.out.println("month=" + c);
                System.out.println("date=" + d);
            } else if (a.length() == 6) {
                String b = a.substring(0, 4);
                String c = a.substring(4, 5);
                String d = a.substring(5, 6);
                System.out.println("year=" + b);
                System.out.println("month=0" + c);
                System.out.println("date=0" + d);
            } else if (a.length() != 6) {
                String b = a.substring(0, 4);
                String c = a.substring(4, 6);
                int month = Integer.parseInt(c);
                String d = a.substring(6, 7);
                if (month > 12) {
                    String mo = a.substring(4, 5);
                    String da = a.substring(5, 7);
                    System.out.println("year=" + b);
                    System.out.println("month=0" + mo);
                    System.out.println("date=" + da);
                } else if (month <= 12) {
                    System.out.println("year=" + b);
                    System.out.println("month=" + c);
                    System.out.println("date=0" + d);
                }
            }
        }

    }
}
全部评论
因为题目输入示例里面三月写的是“03”,默认了 输入输出都会在月和日之前补0
1 回复 分享
发布于 2020-12-18 11:25
输入2010113;这个可能会有两个结果。2010 01 13 和2010 11 03. 可以在最后一个else if 中添加一个条件判断 String e=a.substring(5,7); int date=Integer.parseInt(e); if(date<=31){ String f=a.substring(5,7); System.out.println("year=" + b); System.out.println("month=0" +"1"); System.out.println("date="+f); }
点赞 回复 分享
发布于 2021-09-11 23:30

相关推荐

争当牛马还争不上
码农索隆:1.把简历改哈 2.猛投,狠投 3.把基础打牢 这样你在有机会的时候,才能抓住
点赞 评论 收藏
分享
06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务