根据身份证号判断该人的年龄、性别、出生年月日

根据身份证号判断该人的年龄、性别、出生年月日

问题描述

  • 输入
身份证号输入:123456200101011212
今年是哪一年:2021
  • 输出
该人的性别为:男性
出生年月为:2001年01月01日
年龄为:20周岁

解题思想

  1. 获取信息
  • 信息位置

在这里插入图片描述

  • 获取方法
  1. substring()(获取新的字符串)
    substring()方法连接
    在这里插入图片描述
		String year = (String) id.subSequence(6, 10);
		String month = (String) id.subSequence(10, 12);
		String day = (String) id.subSequence(12, 14);
		String sex = (String) id.subSequence(16, 17);
  1. 将字符串转变成整形 Integer.parseInt()

字符串转变成整形连接

我们会在计算周岁、判断性别的时候,用到加减,所以须将字符串变成整形

		int y = Integer.parseInt(year);
		int s = Integer.parseInt(sex);

完整代码

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入身份证号:");
		String id = sc.next();
		System.out.print("今年是那一年:");
		int n=sc.nextInt();
		String year = (String) id.subSequence(6, 10);
		String month = (String) id.subSequence(10, 12);
		String day = (String) id.subSequence(12, 14);
		String sex = (String) id.subSequence(16, 17);
		int y = Integer.parseInt(year);
		int s = Integer.parseInt(sex);
		System.out.print("该人的性别为:");
		if (s % 2 == 0) {
			System.out.println("女性");
		} else {
			System.out.println("男性");
		}
		System.out.println("出生年月为:" + year + "年" + month + "月" + day + "日");
		System.out.println("年龄为:" + (n - y) + "周岁");
	}
}

运行截图

在这里插入图片描述

  • 9
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值