如何通过在java中创建时钟来解决这个问题?

编写一个程序,从标准输入中读取由小时、分钟和秒组成的时间。用户还可以指定时钟格式(12 小时制,AM/PM 或 24 小时制)。确保对指示的值进行完整性检查(例如,分钟数介于 0 和 59 之间)。


这是我到目前为止所拥有的:


public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter the hours: ");

    int hours = scanner.nextInt();

    System.out.print("Enter the minutes: ");

    int minutes = scanner.nextInt();

    System.out.print("Enter the seconds: ");

    int seconds = scanner.nextInt();

    if (seconds >= 0 && seconds <= 60) {

      minutes = minutes + 1;

      if (minutes >= 60) {

        hours = hours + 1;

        minutes = 00;

        if (hours >= 24) {

          hours = 00;

        }

      }

    }

    System.out.println(seconds + ":" + minutes + ":" + hours);

}


慕无忌1623718
浏览 166回答 3
3回答

富国沪深

如果您使用java.util.Calendar该类,则可以摆脱手动计算。Calendar c = Calendar.getInstance();c.set(Calendar.HOUR_OF_DAY, hours); //24 hour format (10PM is 22)c.set(Calendar.MINUTE, minutes);c.set(Calendar.SECOND, seconds);System.out.println(c.get(Calendar.HOUR_OF_DAY)&nbsp; &nbsp; &nbsp; &nbsp; + ":" + c.get(Calendar.MINUTE)&nbsp; &nbsp; &nbsp; &nbsp; + ":" + c.get(Calendar.SECOND));输出Enter the hours: 20Enter the minutes: 120Enter the seconds: 127622:21:16

米脂

您应该阅读有关整数除法/和余数的信息 %。例如:int seconds = 150;System.out.println("minutes: " + seconds / 60); // minutes: 2System.out.println("seconds: " + seconds % 60); // seconds: 30试试这个代码以了解它是如何工作的:int seconds = 123;int minutes = 120;int hours = 23;int days = 10;minutes += seconds / 60;seconds %= 60;hours += minutes / 60;minutes %= 60;days += hours / 24;hours %= 24;System.out.format("%d days, %02d:%02d:%02d hours\n", days, hours, minutes, seconds);System.exit(0);这将打印出所有正确的时间:11 days, 01:02:03 hours我将在您的代码中添加一些注释:public static void main(String[] args) {&nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; System.out.print("Enter the hours: ");&nbsp; &nbsp; int hours = scanner.nextInt();&nbsp; &nbsp; System.out.print("Enter the minutes: ");&nbsp; &nbsp; int minutes = scanner.nextInt();&nbsp; &nbsp; System.out.print("Enter the seconds: ");&nbsp; &nbsp; int seconds = scanner.nextInt();&nbsp; &nbsp; if (seconds >= 0 && seconds <= 60) {&nbsp; &nbsp; &nbsp; minutes = minutes + 1; // why do you increment minutes here?&nbsp; &nbsp; &nbsp; if (minutes >= 60) {&nbsp; &nbsp; &nbsp; &nbsp; hours = hours + 1; // if minutes is 120, then you'd have to add 2 hours&nbsp; &nbsp; &nbsp; &nbsp; minutes = 00; // if minutes is 61, then you should not set minutes to 0&nbsp; &nbsp; &nbsp; &nbsp; if (hours >= 24) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hours = 00; // same here&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(seconds + ":" + minutes + ":" + hours);}

收到一只叮咚

这是到目前为止的解决方案,我想..public static void main(String[] args) {&nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; System.out.print("What format 12hr or 24hr? [12,24]");&nbsp; &nbsp; int clockFormat = scanner.nextInt();&nbsp; &nbsp; System.out.print("Enter seconds: ");&nbsp; &nbsp; int seconds = scanner.nextInt();&nbsp; &nbsp; System.out.print("Enter minutes: ");&nbsp; &nbsp; int minutes = scanner.nextInt();&nbsp; &nbsp; System.out.print("Enter hours: ");&nbsp; &nbsp; int hours = scanner.nextInt();&nbsp; &nbsp; if (clockFormat == 12)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; while (hours > 12 || hours < 0)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter the hours: ");&nbsp; &nbsp; &nbsp; &nbsp; hours = scanner.nextInt();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; minutes += seconds / 60;&nbsp; &nbsp; seconds %= 60;&nbsp; &nbsp; hours += minutes / 60;&nbsp; &nbsp; minutes %= 60;&nbsp; &nbsp; System.out.format("%02d:%02d:%02d", hours, minutes, seconds);&nbsp; &nbsp; scanner.close();}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java