在教科书中它解释了如何将秒转换为分钟以及剩余多少秒,但我的问题如下。
Write a program that prompts the user to enter the minutes (e.g., 1 billion), and displays the number of years and days for the minutes. For simplicity, assume a year has 365 days. Here is an example.
Enter the number of minutes: 100000000 100000000 minutes is approximately 1902 years and 214 days.
我目前的情况如下:
import java.util.Scanner;
public class Ch2_Exercise2_7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt user for number of minutes
System.out.println("Enter the number of minutes:");
int minutes = input.nextInt();
// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;
System.out.println(minutes + " minutes is " + year + " years and " + remainingMinutes + " days ");
}
}
凭借我所拥有的,它并没有给我剩余的时间。例如,如果我输入 525600 分钟,它会给出 1 年零 365 天,而实际上应该是 1 年。
我正在使用 Java Eclipse。任何帮助将不胜感激!如果我错误地发布了代码,我提前道歉。
最佳答案
你在这里搞砸了一点:
// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;
你把总分钟数除以1440,所以你得到的天数是错误的。您应该取余数然后除以 1440。
另一件事在你的打印声明中。您将一年后剩余的分钟数写为天数。
这应该有效:
// Number of minutes in a year
int year = minutes / 525600;
int remainingMinutes = minutes % 525600;
int day = remainingMinutes / 1440;
System.out.println(minutes + " minutes is approximately " + year + " years and " + day + " days.");
关于java - 如何将分钟变成年和天?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29187451/