写一个程序,输出类似09:03:12的时间格式,需要判断输入的数字是否符合实际情况,比如小时数就不能大于24,如果大于24就提示输入错误;分钟数和秒数不能大于60,如果大于60就提示输入错误。

该程序接收用户输入的小时、分钟和秒,验证输入是否在有效范围内,并将输入转换为24小时制的字符串。如果输入错误,会提示用户重新输入。最后,程序将时间字符串解析为日期对象并展示出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.mlt.zuoye;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class D9 {
    public static void main(String[] args) throws ParseException {
        String rs ="";
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你的时间(小时)(fen)(miao)");
            int a = sc.nextInt();
            int b =sc.nextInt();
            int c =sc.nextInt();
            if (a<=24&&b<=60&&c<=60) {
                rs+=a+":"+b+":"+c;
                break;
            }else {
                System.out.println("输入错误 请重新输入!");
            }
        }
        SimpleDateFormat sdf =new SimpleDateFormat("HH:mm:ss");
        Date d1= sdf.parse(rs);
        System.out.println("输入的时间为:"+sdf.format(d1));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值