java-如何正确添加时间?

huangapple go评论74阅读模式
英文:

java- how to add time properly?

问题

import java.util.Scanner;

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

        int startup_hour;
        int startup_minute;
        int startup_second;

        int add_hours;
        int add_minutes;
        int add_seconds;

        System.out.print("现在是几点(小时)? \n");
        startup_hour = user_input.nextInt();

        System.out.print("现在是几分? \n");
        startup_minute = user_input.nextInt();

        System.out.print("现在是几秒? \n");
        startup_second = user_input.nextInt();

        System.out.println("开始时间为 " + startup_hour
            + " 小时 " + startup_minute + " 分 " + "和 "
            + startup_second + " 秒 \n");

        System.out.print("你想要添加多少小时? \n");
        add_hours = user_input.nextInt();

        System.out.print("你想要添加多少分钟? \n");
        add_minutes = user_input.nextInt();

        System.out.print("你想要添加多少秒? \n");
        add_seconds = user_input.nextInt();

        System.out.println("用户想要添加 " + add_hours
            + " 小时 " + add_minutes + " 分 "
            + "和 " + add_seconds + " 秒 \n");

        int totalHours = (startup_hour + add_hours);
        int totalMinutes = (startup_minute + add_minutes);
        int totalSeconds = (startup_second + add_seconds);

        if (totalSeconds == 60) {
            totalMinutes++;
            totalSeconds = 0;
        }

        if (totalMinutes == 60) {
            totalHours++;
            totalMinutes = 0;
        }

        System.out.println("添加后,时间将为 "
            + totalHours + " 小时 " + totalMinutes + " 分 "
            + totalSeconds + " 秒");
    }
}
英文:

My code isn't working properly for example when a user put that right now is 5 h 43 m and 7 s and the user wanna add 3 h 50 m and 57 s the code compute and shows what will be the time adding but it shows 8 h 93 m and 64 s but I want that after 60 m it shows 9 h 34 m and 4 s so can u help me out.

import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner user_input = new Scanner (System.in);
int startup_hour;
int startup_minute;
int startup_second;
int add_hours;
int add_minutes;
int add_seconds;
System.out.print("what time is it right now(hour)? \n");
startup_hour = user_input.nextInt();
System.out.print("what time is it right now(minutes? \n");
startup_minute = user_input.nextInt();
System.out.print("what time is it right now(seconds)? \n");
startup_second = user_input.nextInt();
System.out.println("The starting time is " + startup_hour
+ " hours " + startup_minute + " minutes " + "and "
+ startup_second + " seconds \n");
System.out.print("How many hours you wanna add? \n");
add_hours = user_input.nextInt();
System.out.print("How many minutes you wanna add? \n");
add_minutes = user_input.nextInt();
System.out.print("How many seconds you wanna add? \n");
add_seconds = user_input.nextInt();
System.out.println("The user wanna add " + add_hours
+ " hours " + add_minutes + " minutes "
+ "and " + add_seconds + " seconds \n");
int totalHours = (startup_hour + add_hours);
int totalMinutes = (startup_minute + add_minutes);
int totalSeconds = (startup_second + add_seconds);
if (totalSeconds == 60){
totalMinutes++; 
totalSeconds = 0;
}
if (totalMinutes == 60){
totalHours++;
totalMinutes = 0;
}
System.out.println("After adding, the time would then be "
+ totalHours + " hours " + totalMinutes + " Minutes "
+ totalSeconds + " Seconds ");
*emphasized text*
}
}

thanku

答案1

得分: 1

以下是翻译好的内容:

你的程序不工作的原因是因为你在处理时间时没有使用标准单位,即

例如

假设启动时间为**1小时3分钟57秒
用户想要添加
1小时57分钟3秒**。

正确的答案应该是**3小时1分钟0秒,但你的程序会返回2小时61分钟0秒**。

那么,为什么会发生这种情况呢?

原因如下:

  • 如前所述,你没有使用标准单位()处理时间。
  • 你的if循环中的条件不正确。你只检查了分钟/秒是否等于60。如果分钟是61或更多,怎么办?

解决方案:

> 最简单的解决方案是,首先将时间转换为,添加所需的时间,然后将其转换回小时:分钟:秒。如果使用处理时间,甚至不需要使用if循环。

以下是修改后的代码,可以正常工作:

import java.util.Scanner;
    
public class Test 
{

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

        int startup_hour;
        int startup_minute;
        int startup_second;
        int add_hours;
        int add_minutes;
        int add_seconds;

        System.out.print("What time is it right now(hour)    : ");
        startup_hour = user_input.nextInt();

        System.out.print("What time is it right now(minutes) : ");
        startup_minute = user_input.nextInt();

        System.out.print("What time is it right now(seconds) : ");
        startup_second = user_input.nextInt();

        System.out.println("The starting time is " + startup_hour + " hours " + startup_minute + " minutes "
                + "and " + startup_second + " seconds.");
        System.out.println();

        System.out.print("How many hours you wanna add   : ");
        add_hours = user_input.nextInt();

        System.out.print("How many minutes you wanna add : ");
        add_minutes = user_input.nextInt();

        System.out.print("How many seconds you wanna add : ");
        add_seconds = user_input.nextInt();

        System.out.println("The user wanna add " + add_hours + " hours " + add_minutes + " minutes "
                + "and " + add_seconds + " seconds.");
        System.out.println();

        int totalSecondsAtStart = (startup_hour * 60 * 60) + (startup_minute * 60) + startup_second;
        int totalSecondsToAdd = (add_hours * 60 * 60) + (add_minutes * 60) + (add_seconds);
        int totalSeconds = totalSecondsAtStart + totalSecondsToAdd;

        //Convert total seconds to hour, minutes and seconds;
        int totalMinutes = (totalSeconds / 60);
        int totalHours = (totalMinutes / 60);

        int finalHours = totalHours;
        int finalMinutes = totalMinutes - (totalHours * 60);
        int finalSeconds = totalSeconds - (totalMinutes * 60);

        System.out.println("After adding, the time would then be " + finalHours + " hours"
          + " " + finalMinutes + " Minutes " + finalSeconds + " Seconds.");
    }
}

注意我是如何将时间转换回小时:分钟:秒的。

英文:

The reason your program is not working because you are processing time, without using the standard unit, which is seconds.

For example:

Suppose the start up time is 1 hours 3 minutes and 57 seconds.
And the user want to add, 1 hour 57 minutes and 3 seconds.

The correct answer will be, 3 hours 1 Minutes 0 Seconds but your program will return 2 hours 61 Minutes 0 Seconds.

Now, why did this happen?

The reason are:

  • As already stated, you did not process time using the standard unit (seconds).
  • The condition in your if loop is not correct. You are only checking if the minutes/seconds are equal to 60. What if the minutes or seconds are 61 or more?

Solution:

> The simplest solution is, first convert time into seconds, add how much time you want to add, then convert it back to hours:minutes:seconds. You won't even have to use if loop if you process time using seconds.

Here is the modified code, which works properly :

import java.util.Scanner;
public class Test 
{
public static void main(String[] args)
{
Scanner user_input = new Scanner(System.in);
int startup_hour;
int startup_minute;
int startup_second;
int add_hours;
int add_minutes;
int add_seconds;
System.out.print("What time is it right now(hour)    : ");
startup_hour = user_input.nextInt();
System.out.print("What time is it right now(minutes) : ");
startup_minute = user_input.nextInt();
System.out.print("What time is it right now(seconds) : ");
startup_second = user_input.nextInt();
System.out.println("The starting time is " + startup_hour + " hours " + startup_minute + " minutes "
+ "and " + startup_second + " seconds.");
System.out.println();
System.out.print("How many hours you wanna add   : ");
add_hours = user_input.nextInt();
System.out.print("How many minutes you wanna add : ");
add_minutes = user_input.nextInt();
System.out.print("How many seconds you wanna add : ");
add_seconds = user_input.nextInt();
System.out.println("The user wanna add " + add_hours + " hours " + add_minutes + " minutes "
+ "and " + add_seconds + " seconds.");
System.out.println();
int totalSecondsAtStart = (startup_hour * 60 * 60) + (startup_minute * 60) + startup_second;
int totalSecondsToAdd = (add_hours * 60 * 60) + (add_minutes * 60) + (add_seconds);
int totalSeconds = totalSecondsAtStart + totalSecondsToAdd;
//Convert total seconds to hour, minutes and seconds;
int totalMinutes = (totalSeconds / 60);
int totalHours = (totalMinutes / 60);
int finalHours = totalHours;
int finalMinutes = totalMinutes - (totalHours * 60);
int finalSeconds = totalSeconds - (totalMinutes * 60);
System.out.println("After adding, the time would then be " + finalHours + " hours"
+ " " + finalMinutes + " Minutes " + finalSeconds + " Seconds.");
}
}

Notice how I converted time back into hours:minutes:seconds.

huangapple
  • 本文由 发表于 2020年7月26日 09:05:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63095123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定