创建一个接受原始输入而不是在数据计算后使用数据的方法应该如何实现?

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

How would I create a method that accepts the raw input instead of using data post-calculation?

问题

我正在尝试编写一个程序它会显示给定特定小时/分钟/秒输入以及长秒输入的小时/分钟/秒和经过的时间到目前为止它显示了正确的小时分钟和秒然而经过的时间使用了计算完成后的数据

例如如果我将小时/分钟/秒输入为331 34 674它会返回19小时45分钟14秒这是预期的。(我不显示天数所以我只是取 hr % 24)。然而当我要求以秒为单位的经过时间时它返回71114等于19小时45分钟14秒),而不是预期的1194314331小时45分钟14秒)。为什么如何修复我已经附上了以下代码
英文:

I am trying to write a program that shows the hour/min/sec and elapsed time given a specific hour/min/sec input as well as a long sec input. So far, I have it showing the correct hour, minute, and second. However, the elapsed time is using the data after the calculations have completed.

For example, if I input hours/mins/sec as 331 34 674, it returns 19 hours 45 minutes 14 seconds, as it is supposed to. (I am not showing days, so I'm just taking hr % 24. However, when I asked for the elapsed time in seconds, it returns 71114 (equal to 19 hours 45 minutes 14 seconds) instead of the expected 1194314 (331 hours 45 minutes 14 seconds). Why? How do I fix? I have attached the code below.

package CHTime;


import java.util.Scanner;
public class CHTest 
{

	public static void main(String[] args) 
	{
		// Declare variables
		Scanner chIn = new Scanner(System.in);
				
		// Prompt user for input time1
		System.out.println("Enter time1 (hour minute second): ");
		int chHr = chIn.nextInt();
		int chMin = chIn.nextInt();
		int chSec = chIn.nextInt();
		CHTime chTime1 = new CHTime(chHr, chMin, chSec);
		
		// Display Results
		System.out.println(chTime1.toString());
		
		// Display elapsed time in time1
		System.out.println("Elapsed seconds in time1: " + chTime1.getSeconds());
		
		
		// Prompt user for elapsed time
		System.out.println("Enter time2 (elapsed time): ");
		CHTime chTime2 = new CHTime(chIn.nextLong());
		
		// Display results
		System.out.println(chTime2.toString());
		
		// Display elapsed time in time 2
		System.out.println("Elapsed seconds in time2: " + chTime2.getSeconds());
	}

}

package CHTime;

public class CHTime implements Comparable<CHTime>
{
	// Data field for elapsed time since midnight Jan 1, 1970
	private long chUnix = Instant.now().getEpochSecond();
	
	// Data fields for hour, min, and sec
	private int chHr;
	private int chMin;
	private int chSec;
		
	
	// Constructor with specified hour, min, and sec to create a time
	public CHTime(int chHr, int chMin, int chSec)
	{
		this.chSec = chSec % 60;
		this.chHr = chHr % 24;
		this.chMin = (chMin + chSec / 60) % 60;
	}
	
	// Constructor with specified time since Unix
	public CHTime(long chElapsedTime)
	{
		this.chSec = (int)(chElapsedTime) % 60;
		chHr = (int)(chElapsedTime / 60);
		this.chMin = chHr % 60;
		this.chHr = (chHr / 60) % 24;
	}
	
	// Create method that returns the hour in range 0-23
	public int getHour()
	{
		return chHr;
	}
	
	// Create method that returns the minute in range 0 - 59
	public int getMinute()
	{
		return chMin;
	}
	
	// Create method that returns the second in range 0 - 59
	public int getSecond()
	{
		return chSec;
	}
	
	// Create method that returns the elapsed total seconds
	public long getSeconds()
	{
		return (chHr * 3600) + (chMin * 60) + chSec;
	}
	
	@Override // Create a method that returns a string showing time
	public String toString()
	{
		return chHr + " hours " + chMin + " minutes " + chSec + " seconds";
	}

答案1

得分: 1

你在计算 chHr、chMin 和 chSec 之后运行了 getSeconds() 函数,所以它们分别是 19、45 和 14,而不是 331、34 和 674。

我的建议:

你可以在构造函数中先计算秒数,并将其作为参数保留,然后再计算其他值。

或者保留构造函数中给定的原始值,并在 getSeconds 函数中使用它们。

英文:

You are running the function getSeconds() after you calculate chHr, chMin, and chSec. so they are 19, 45, and 14 respectively, and not 331, 34, and 674.

my suggestion:

You could either calculate the seconds in the constructor and keep it as a parameter before calculating the others.

Or keep the original values given in the constructor and use them in the getSeconds function.

答案2

得分: 0

看一下期望值和实际值之间的差异,以秒为例:实际值为71114,期望值为1194314,差异正好是13天,这是你取模后去掉的部分。

你的代码完全没问题,问题在于你的期望有误:天数被去掉了,所以不会神奇地重新出现 创建一个接受原始输入而不是在数据计算后使用数据的方法应该如何实现?

请停止将函数参数和成员变量命名相同的习惯,使用 "this" 来区分它们。以这种方式来做,你的生活将不会幸福。通过给它们取一个明显不同的名字,比如 "p_chMin",可以清晰地区分参数。

按照 "你的方式" 做会存在一种风险,有可能会意外忘记其中的一个 "this",将你的数据写入参数(易变)而不是类成员(持久)。

英文:

look at the difference of the expected and the received value for second: the difference between 71114 (received) and 1194314 (expected) is exectly the 13 days, that you modulo'ed away.

Your code is absolutely OK, its your expectation that is wrong: days are stripped, so you won't get them back magically 创建一个接受原始输入而不是在数据计算后使用数据的方法应该如何实现?

And please quit your habit of naming function params and member variables identically and use "this" to tell between them. You will not get a happy live this way. make your params clearly distinguishable by e.g. naming them e.g. "p_chMin".
Doing it "your way" caries the risk to accidentally forget one the many this'es and writing your data into a param (volatile) instead of the class member (persistent).

答案3

得分: 0

以下是代码部分的中文翻译:

如果您需要存储时秒的初始值我建议将其转换为单个Instant值然后在其上调用相应的方法

public class CHTime implements Comparable<CHTime> {
    private final Instant chInstant;

    public CHTime(int chHr, int chMin, int chSec) {
        this.chInstant = Instant.ofEpochSecond((chHr * 3600) + (chMin * 60) + chSec);
    }

    public CHTime(long chElapsedTime) {
        this.chInstant = Instant.ofEpochSecond(chElapsedTime);
    }

    public int getHour() {
        int hours = chInstant.atZone(ZoneId.systemDefault()).getHour() - 1;
        return hours < 0 ? 23 : hours;
    }

    public int getMinute() {
        return chInstant.atZone(ZoneId.systemDefault()).getMinute();
    }

    public int getSecond() {
        return chInstant.atZone(ZoneId.systemDefault()).getSecond();
    }

    public long getSeconds() {
        return chInstant.getEpochSecond();
    }

    @Override
    public String toString() {
        return getHour() + "小时" + getMinute() + "分钟" + getSecond() + "秒";
    }
}

此外,以下是第一个构造函数的中文翻译:

public CHTime(int chHr, int chMin, int chSec)
{
    this.chSec = chSec % 60;
    this.chHr = chHr % 24;
    this.chMin = (chMin + chSec / 60) % 60;
}

这个构造函数从秒数中添加分钟,但对于小时没有执行相同的操作,这是不一致的。使用Instant可以解决这个问题。

第一个构造函数中文翻译的最后一部分可能需要调整,具体情况取决于上下文。希望这些翻译对您有帮助。

英文:

If you need to store initial value of epoch seconds hours/minutes/seconds i suggest convert it to single Instant value and just call respective methods on it.

public class CHTime implements Comparable&lt;CHTime&gt; {
        private final Instant chInstant;
    
        public CHTime(int chHr, int chMin, int chSec) {
            this.chInstant = Instant.ofEpochSecond((chHr * 3600) + (chMin * 60) + chSec);
        }
    
        public CHTime(long chElapsedTime) {
            this.chInstant = Instant.ofEpochSecond(chElapsedTime);
        }
    
        public int getHour() {
            int hours = chInstant.atZone(ZoneId.systemDefault()).getHour() - 1;
            return hours &lt; 0 ? 23 : hours;
        }
    
        public int getMinute() {
            return chInstant.atZone(ZoneId.systemDefault()).getMinute();
        }
    
        public int getSecond() {
            return chInstant.atZone(ZoneId.systemDefault()).getSecond();
        }
    
        public long getSeconds() {
            return chInstant.getEpochSecond();
        }
    
        @Override
        public String toString() {
            return getHour() + &quot; hours &quot; + getMinute() + &quot; minutes &quot; + getSeconds() + &quot; seconds&quot;;
        }
    }

Also this constructor:

public CHTime(int chHr, int chMin, int chSec)
    {
        this.chSec = chSec % 60;
        this.chHr = chHr % 24;
        this.chMin = (chMin + chSec / 60) % 60;
    }

Adds minutes from seconds, but doesn't do same for hours, which is inconsistent. Using Instant would solve this problem.

Edit 1

ZonedDateTime getHour() method for 00:00:00-00:59:59 will return 1, and for 23:00:00-23:59:59 will return 0. I edited my answer to take that into consideration.

huangapple
  • 本文由 发表于 2023年2月24日 05:24:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550455.html
匿名

发表评论

匿名网友

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

确定