Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将过去的小时数转换为当前时间_Java - Fatal编程技术网

Java 将过去的小时数转换为当前时间

Java 将过去的小时数转换为当前时间,java,Java,我有一个开始时间(在本例中是9点),并且有一定数量的小时被添加到其中,然后需要计算当前时间。我目前使用的方法是将经过的小时数与原始时间相加,然后除以12,如下所示:timeAndHoursPassed%12=currentTime这在所有情况下都很有效,除了添加的时间可被12整除,在这种情况下,我将当前时间设为零,而不是12。我怎样才能解决这个问题?此外,如果可能的话,我更愿意使用一些基本的数学,而不是使用GregorianCalendar类。 提前感谢您的帮助。 我的代码如下: package

我有一个开始时间(在本例中是9点),并且有一定数量的小时被添加到其中,然后需要计算当前时间。我目前使用的方法是将经过的小时数与原始时间相加,然后除以12,如下所示:
timeAndHoursPassed%12=currentTime
这在所有情况下都很有效,除了添加的时间可被12整除,在这种情况下,我将当前时间设为零,而不是12。我怎样才能解决这个问题?此外,如果可能的话,我更愿意使用一些基本的数学,而不是使用GregorianCalendar类。 提前感谢您的帮助。 我的代码如下:

package week11;

import java.util.Scanner;

public class PassingTrains {
    static int currentTime = 9, firstDistance = 0, secondDistance = 0, firstSpeed = 40, secondSpeed;
    static Scanner input = new Scanner(System.in);
    public static String passTime(){
        System.out.print("Enter the speed of the passenger train: ");
        secondSpeed = input.nextInt();
        while (currentTime < 11) {
            firstDistance += firstSpeed;
            currentTime++;
        }
        while (firstDistance > secondDistance) {
            firstDistance += firstSpeed;
            secondDistance += secondSpeed;
            currentTime++;
        }
        if (firstDistance == secondDistance){
            return ("at " + currentTime % 12);
        } else {
            return ("between " + (currentTime - 1) % 12 + " o'clock and " + currentTime % 12);
        }
    }

    public static void main(String[] args) {
            System.out.println("The passenger train passed the freight train at " + passTime() + " o'clock");
            System.out.println("The freight train was traveling " + firstSpeed + " mph");
            System.out.println("The passenger train was traveling at " + secondSpeed + " mph");
    }
}
packageweek11;
导入java.util.Scanner;
公共类通行证{
静态int currentTime=9,firstDistance=0,secondDistance=0,firstSpeed=40,secondSpeed;
静态扫描仪输入=新扫描仪(System.in);
公共静态字符串passTime(){
系统输出打印(“输入旅客列车的速度:”);
secondSpeed=input.nextInt();
而(当前时间<11){
第一距离+=第一速度;
currentTime++;
}
同时(第一距离>第二距离){
第一距离+=第一速度;
第二距离+=第二速度;
currentTime++;
}
如果(第一距离==第二距离){
返回(“在”+当前时间%12);
}否则{
返回(“在“+(当前时间-1)%12+”点和“+当前时间%12”之间);
}
}
公共静态void main(字符串[]args){
System.out.println(“客车在“+passTime()+”点钟”通过货车”);
System.out.println(“货物列车正在行驶”+firstSpeed+mph”);
System.out.println(“客车以“+秒速度+”英里/小时”行驶);
}
}
有两个部分:

  • 必须对经过的时间应用模24。为什么?如果你今天从9点开始,24小时过去了,你加上0小时(24%24),第二天就有9点了
  • 让我们假设已经过去了37年。申请上述#1,24小时后你还有13小时。加上13到9,你有22小时(晚上10点)。如果您不想使用24小时日历,如果当前时间>12,请从当前时间中减去12
  • 示例代码:

    currentTime += elapsedTime % 24; 
    if(currentTime > 12) 
    {
        currentTime -= 12;
    }
    
    更新:OP的更正代码

    public class PassingTrains
    {
        static int currentTime = 9, firstDistance = 0, secondDistance = 0,
                firstSpeed = 40, secondSpeed, elapsedTime;
        static Scanner input = new Scanner(System.in);
    
        public static String passTime()
        {
            System.out.print("Enter the speed of the passenger train: ");
            secondSpeed = input.nextInt();
            while (currentTime < 11)
            {
                firstDistance += firstSpeed;
                currentTime++;
            }
            while (firstDistance > secondDistance)
            {
                firstDistance += firstSpeed;
                secondDistance += secondSpeed;
                elapsedTime++;  // changed this
    
            }
            // added the next two lines
            currentTime += elapsedTime % 24;
            currentTime = (currentTime > 12) ? currentTime - 12 : currentTime;
    
            if (firstDistance == secondDistance)
            {
                return ("at " + currentTime); // fixed this
            }
            else
            {
                return ("between " + ((currentTime - 1 == 0) ? 12 : currentTime - 1) + " o'clock and " + currentTime); \\fixed this
            }
        }
    
        public static void main(String[] args)
        {
            System.out.println("The passenger train passed the freight train at "
                    + passTime() + " o'clock");
            System.out.println("The freight train was traveling " + firstSpeed
                    + " mph");
            System.out.println("The passenger train was traveling at "
                    + secondSpeed + " mph");
        }
    }
    
    公共类PassingTrains
    {
    静态int currentTime=9,firstDistance=0,secondDistance=0,
    firstSpeed=40,secondSpeed,elapsedTime;
    静态扫描仪输入=新扫描仪(System.in);
    公共静态字符串passTime()
    {
    系统输出打印(“输入旅客列车的速度:”);
    secondSpeed=input.nextInt();
    而(当前时间<11)
    {
    第一距离+=第一速度;
    currentTime++;
    }
    同时(第一距离>第二距离)
    {
    第一距离+=第一速度;
    第二距离+=第二速度;
    elapsedTime++;//更改了此设置
    }
    //添加了接下来的两行
    currentTime+=elapsedTime%24;
    currentTime=(currentTime>12)?currentTime-12:currentTime;
    如果(第一距离==第二距离)
    {
    return(“在”+当前时间);//修复了此问题
    }
    其他的
    {
    return(“介于”+((currentTime-1==0)12:currentTime-1)+“0点钟和”+currentTime)之间;\\r修复了此问题
    }
    }
    公共静态void main(字符串[]args)
    {
    System.out.println(“旅客列车在点通过货运列车”
    +密码时间();
    System.out.println(“货物列车正在行驶”+第一速度
    +“英里/小时”);
    System.out.println(“旅客列车在”
    +第二速度+“mph”);
    }
    }
    
    首先,发布您的代码。@JasonC该建议不起作用。@hfontanez发布编辑,不,它不起作用。谢谢你的ping。