如何在Java中创建时钟来解决此问题?

时间:2018-10-06 18:57:54

标签: java eclipse

编写一个程序,该程序从标准输入中读取包含小时,分钟和秒的时间。用户还可以指定 时钟格式(带有AM / PM的12小时制或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);
}

4 个答案:

答案 0 :(得分:1)

您应该阅读整数除法/和余数%。例如:

int seconds = 150;
System.out.println("minutes: " + seconds / 60); // minutes: 2
System.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) {
    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; // why do you increment minutes here?
      if (minutes >= 60) {
        hours = hours + 1; // if minutes is 120, then you'd have to add 2 hours
        minutes = 00; // if minutes is 61, then you should not set minutes to 0
        if (hours >= 24) {
          hours = 00; // same here
        }
      }
    }
    System.out.println(seconds + ":" + minutes + ":" + hours);
}

答案 1 :(得分:1)

如果使用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)
        + ":" + c.get(Calendar.MINUTE)
        + ":" + c.get(Calendar.SECOND));

输出

Enter the hours: 20
Enter the minutes: 120
Enter the seconds: 1276

22:21:16

答案 2 :(得分:0)

这是我想出的解决方案。我省略了一些内容,因为这显然是功课,您需要学习。这将使您对需要进行的逻辑有所了解。

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("What format 12hr or 24hr? [12,24]");
    int format - scanner.nextInt();
    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 (format == 12)
    {
      //do this code
      if (hours > 12 || hours < 0)
      {
        //do this code
        //accept input until hours is less than or equal to 12
        System.out.print("Enter the hours: ");
        hours = scanner.nextInt();
      }
      if (minutes > 60 || minutes < 0)
      {
        //do this code
        //accept input until minutes is less than or equal to 60
      }
      if (seconds > 60 || seconds < 0)
      {
        //do this code
        //accept input until seconds is less than or equal to 60
      }
          System.out.println(seconds + ":" + minutes + ":" + hours);
    }

    if (format == 24)
    {
      //do this code
      if (hours > 24 || hours < 0)
      {
        //do this code
        //accept input until hours is less than or equal to 24
      }
      if (minutes > 60 || minutes < 0)
      {
        //do this code
        //accept input until minutes is less than or equal to 60
      }
      if (seconds > 60 || seconds < 0)
      {
        //do this code
        //accept input until seconds is less than or equal to 60
      }
          System.out.println(seconds + ":" + minutes + ":" + hours);
    }

答案 3 :(得分:0)

我想这是到目前为止的解决方案。

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.print("What format 12hr or 24hr? [12,24]");
    int clockFormat = scanner.nextInt();

    System.out.print("Enter seconds: ");
    int seconds = scanner.nextInt();

    System.out.print("Enter minutes: ");
    int minutes = scanner.nextInt();

    System.out.print("Enter hours: ");
    int hours = scanner.nextInt();
    if (clockFormat == 12)
    {

      while (hours > 12 || hours < 0)
      {

        System.out.print("Enter the hours: ");
        hours = scanner.nextInt();
      }
    }
    minutes += seconds / 60;
    seconds %= 60;
    hours += minutes / 60;
    minutes %= 60;

    System.out.format("%02d:%02d:%02d", hours, minutes, seconds);

    scanner.close();
}

}