"输出应如下所示(回显输入是个好主意):您输入了500,000秒,即5天,18小时,53分钟和20秒。 (5天18:53:20小时)
那么你们可以告诉我应该怎么做,理解和做到最简单的方法是什么,请帮帮我们,我真的卡住了,他说'#34;没有硬编码&#34 ;我并不完全确定它是什么,但我认为他希望我们为它们分配常量。
答案 0 :(得分:26)
使用内置TimeUnit
。
long uptime = System.currentTimeMillis();
long days = TimeUnit.MILLISECONDS
.toDays(uptime);
uptime -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS
.toHours(uptime);
uptime -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS
.toMinutes(uptime);
uptime -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS
.toSeconds(uptime);
答案 1 :(得分:14)
我的基本java算术计算的快速答案是:
首先考虑以下值:
1 Minute = 60 Seconds
1 Hour = 3600 Seconds ( 60 * 60 )
1 Day = 86400 Second ( 24 * 3600 )
代码段如下:
int input=500000;
int numberOfDays;
int numberOfHours;
int numberOfMinutes;
int numberOfSeconds;
numberOfDays = input / 86400;
numberOfHours = (input % 86400 ) / 3600 ;
numberOfMinutes = ((input % 86400 ) % 3600 ) / 60
numberOfSeconds = ((input % 86400 ) % 3600 ) % 60 ;
我希望能提供帮助 给你。
答案 2 :(得分:4)
应该是这样的:
public static void calculateTime(long seconds) {
int day = (int)TimeUnit.SECONDS.toDays(seconds);
long hours = TimeUnit.SECONDS.toHours(seconds) - (day *24);
long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60);
long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) *60);
System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second);
}
说明:
TimeUnit.SECONDS.toHours(秒)将为您提供从秒到小时的直接转换,而不考虑天数。 减去你已经有几天的时间,即白天* 24。你现在剩下几个小时了。 分钟和秒钟相同。你需要分别减去已经得到的小时和分钟。
答案 3 :(得分:3)
你应该试试这个
import java.util.Scanner;
public class Time_converter {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int seconds;
int minutes ;
int hours;
System.out.print("Enter the number of seconds : ");
seconds = input.nextInt();
hours = seconds / 3600;
minutes = (seconds%3600)/60;
int seconds_output = (seconds% 3600)%60;
System.out.println("The time entered in hours,minutes and seconds is:");
System.out.println(hours + " hours :" + minutes + " minutes:" + seconds_output +" seconds");
}
}
答案 4 :(得分:2)
最简单的方式
Scanner in = new Scanner(System.in);
System.out.println("Enter seconds");
int s = in.nextInt();
int sec = s % 60;
int min = (s / 60)%60;
int hours = (s/60)/60;
System.out.println(hours + ":" + min + ":" + sec);
答案 5 :(得分:0)
看看课程
org.joda.time.DateTime
这使您可以执行以下操作:
old = new DateTime();
new = old.plusSeconds(500000);
System.out.println("Hours: " + (new.Hours() - old.Hours()));
但是,您的解决方案可能更简单:
您需要计算一天中的秒数,将输入除以结果以获取天数,然后从输入中减去它以保留余数。 然后你需要计算剩下的几小时, 分钟后, 最后的余数是秒。
这是为您完成的分析,现在您可以专注于代码。
你需要询问他/她的意思是什么,没有硬编码",通常它意味着传递参数,而不是固定输入值。有很多方法可以执行此操作,具体取决于您运行代码的方式。属性是java中的常用方法。
答案 6 :(得分:0)
您可以使用Java enum TimeUnit
来执行数学运算并避免使用任何硬编码值。然后,我们可以使用String.format(String, Object...)
和一对StringBuilder
(s)以及DecimalFormat
来构建请求的输出。像,
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a number of seconds:");
String str = scanner.nextLine().replace("\\,", "").trim();
long secondsIn = Long.parseLong(str);
long dayCount = TimeUnit.SECONDS.toDays(secondsIn);
long secondsCount = secondsIn - TimeUnit.DAYS.toSeconds(dayCount);
long hourCount = TimeUnit.SECONDS.toHours(secondsCount);
secondsCount -= TimeUnit.HOURS.toSeconds(hourCount);
long minutesCount = TimeUnit.SECONDS.toMinutes(secondsCount);
secondsCount -= TimeUnit.MINUTES.toSeconds(minutesCount);
StringBuilder sb = new StringBuilder();
sb.append(String.format("%d %s, ", dayCount, (dayCount == 1) ? "day"
: "days"));
StringBuilder sb2 = new StringBuilder();
sb2.append(sb.toString());
sb2.append(String.format("%02d:%02d:%02d %s", hourCount, minutesCount,
secondsCount, (hourCount == 1) ? "hour" : "hours"));
sb.append(String.format("%d %s, ", hourCount, (hourCount == 1) ? "hour"
: "hours"));
sb.append(String.format("%d %s and ", minutesCount,
(minutesCount == 1) ? "minute" : "minutes"));
sb.append(String.format("%d %s.", secondsCount,
(secondsCount == 1) ? "second" : "seconds"));
System.out.printf("You entered %s seconds, which is %s (%s)%n",
new DecimalFormat("#,###").format(secondsIn), sb, sb2);
当我输入500000
时输出所请求的(为帖子添加的手动换行符) -
You entered 500,000 seconds, which is 5 days, 18 hours,
53 minutes and 20 seconds. (5 days, 18:53:20 hours)
答案 7 :(得分:0)
谢谢大家的帮助,我真的很感激,但我实际上已经做了一些思考,并开始做一些伪代码,并提出了这个。
import java.util.Scanner;
公共类项目{
public static void main(String[] args) {
//variable declaration
Scanner scan = new Scanner(System.in);
final int MIN = 60, HRS = 3600, DYS = 84600;
int input, days, seconds, minutes, hours, rDays, rHours;
//input
System.out.println("Enter amount of seconds!");
input = scan.nextInt();
//calculations
days = input/DYS;
rDays = input%DYS;
hours = rDays/HRS;
rHours = rDays%HRS;
minutes = rHours/MIN;
seconds = rHours%MIN;
//output
if (input >= DYS) {
System.out.println(input + " seconds equals to " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds");
}
else if (input >= HRS && input < DYS) {
System.out.println(input + " seconds equals to " + hours + " hours " + minutes + " minutes " + seconds + " seconds");
}
else if (input >= MIN && input < HRS) {
System.out.println(input + " seconds equals to " + minutes + " minutes " + seconds + " seconds");
}
else if (input < MIN) {
System.out.println(input + " seconds equals to seconds");
}
scan.close();
}
我知道它看起来真的很无聊但请记住我不仅仅是Java,而是完全编程,而且知道伪代码的人确实非常有帮助。