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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29187451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:50:28  来源:igfitidea点击:

How to turn minutes into years and days?

java

提问by Michael

In the textbook it explains how to convert seconds into minutes with how many seconds remain but the question I have is as follows.

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

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.

编写一个程序,提示用户输入分钟(例如,10 亿),并显示分钟的年数和天数。为简单起见,假设一年有 365 天。这是一个例子。

Enter the number of minutes: 100000000
100000000 minutes is approximately 1902 years and 214 days.

What I currently have is as follows:

我目前的情况如下:

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 ");
    }

   }

With what I have it isn't giving me the remaining minutes into days. For example, if I put in 525600 minutes it gives me 1 year and 365 days when it should just be 1 year.

凭借我所拥有的,它并没有给我剩下的几分钟。例如,如果我输入 525600 分钟,它会给我 1 年零 365 天,而它应该只是 1 年。

I'm using Java Eclipse. Any help would be greatly appreciated! My apologies in advance if I post the code incorrectly.

我正在使用 Java Eclipse。任何帮助将不胜感激!如果我错误地发布了代码,我提前道歉。

回答by mmking

You screwed up a bit over here:

你在这里搞砸了一点:

// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;

You took the total number of minutes and divided by 1440, so the number of days you got was wrong. You should have taken the remainder and then divided by 1440.

你用总分钟数除以 1440,所以你得到的天数是错误的。您应该取余数然后除以 1440。

Another thing was in your print statement. You wrote the number of minutes left after one year as the number of days.

另一件事是在你的印刷声明中。您将一年后剩余的分钟数写为天数。

This should work:

这应该有效:

// 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.");

回答by Dewan Raiyan Uddin Ahmed

int mins,hours,remainingDays,days,years;

cout << "Enter minutes:";
cin >> mins;

hours = mins/60;
days = hours/24;
years = days/365;

remainingDays = days % 365;

cout << years << " years " << remainingDays << " days " << endl;

return 0;