java - 如何将分钟变成年和天?

标签 java

在教科书中它解释了如何将秒转换为分钟以及剩余多少秒,但我的问题如下。

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/

相关文章:

java - Oracle ADF 11g 在提交更改之前验证每个实体实现

java - AffineTransform 翻译不同的图形对象

java - boolean 运算符的差异 : & vs && and | vs ||

java - 如何从 Intellij IDEA 访问内存中的 h2 数据库

java - 如何在 Android 模拟器中获取错误堆栈

JavaFX 图像未在舞台上显示

java - Android 开发人员 - 应用程序停止工作,代码中没有错误

java - 如何在java中打印二维数组游戏板中的行号

java - SpringMVC中,提交表单后,遇到PropertyNotFoundException

java - 在java中使用正则表达式提取两个特定单词之间的子字符串