Java小强在统计一个小区里居民的出生年月,但是发现大家填写的生日格式不统一,例如有的人写 199808,有的人只写 9808。有强迫症的小强请你写个程序,把所有人的出生年月都整理成 年年年年-月月

小强在统计一个小区里居民的出生年月,但是发现大家填写的生日格式不统一,例如有的人写 199808,有的人只写 9808。有强迫症的小强请你写个程序,把所有人的出生年月都整理成 年年年年-月月 格式。对于那些只写了年份后两位的信息,我们默认小于 22 都是 20 开头的,其他都是 19 开头的。

import java.util.Scanner;
class Main{
    public static void main(String []args){
        Scanner scanner = new Scanner(System.in);
        String x = scanner.nextLine();
        if (x.length()==6){
            String years =x .substring(0,4);
            String month = x.substring(4);
            System.out.println(years+"-"+month);
        }else{
            String years =x.substring(0,2);
            String month =x.substring(2);
            int year = Integer.parseInt(years.substring(0));
            if (year>=22){
                System.out.print("19"+years+"-"+month);
            }else{
                System.out.print("20"+years+"-"+month);
            }
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_53756771/article/details/120922415