2

这是我的代码:

public class numberOfYears {
    public static void main(String args[]){
    String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
    double mins = Double.parseDouble(minsString);
    //calcuate mins in a year
    double minsOfaYear = 365*24*60;
    double total = mins / minsOfaYear;
    double year = total / 10;
    double day = total / 10;

 System.out.println(year+ " years and " + day + " days");
 //result isnt right

    }
}

问题基本上是要求输入分钟,并且程序应该将其计算为年和天。例如,如果我输入 1000000000 分钟,它应该给出 1902 年和 214 天的结果。但我没有得到正确的结果. 有人可以为我指出问题吗?谢谢!

4

4 回答 4

1

您应该将剩余时间转换为更小的时间单位。例如,余下的年份应转换为天数,余下的天​​数应转换为小时数等。

你能试试这个:

public class numberOfYears {
    public static void main(String args[]){
    String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
    int mins = Int.parseInt(minsString, 16);

    String time = ConvertTime(mins);

    System.out.println(time);
    }
}   

// Convert minutes to years, days, hours an minutes
public String ConvertTime(int time){
   String result = "";

   int years = time/(365*24*60);
   int days = (time%(365*24*60))/(24*60);
   int hours= (time%(365*24*60)) / 60;
   int minutes = (time%(365*24*60)) % 60;

   result = years+" years " + days + " days "+ hours + " hours " + minutes + " minutes";
   return result;
 }
于 2013-05-01T03:59:38.953 回答
0

对于这种数学问题,我们需要使用整数除法。如果分钟数计算为 45.8 年,您希望将其计算为 45 年,然后将剩余的 0.8 年转换为天数。我不清楚为什么你的代码有/ 10

试试这个:

public class numberOfYears {
   static double minsOfaYear = 365*24*60;

   public static void main(String args[]) {
      String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
      double mins = Double.parseDouble(minsString);
      //calcuate mins in a year
      int year = (int) (mins / minsOfaYear);
      double day = mins - year*minsOfaYear;
      System.out.println(year+ " years and " + day + " days");
   }
}
于 2013-05-01T03:54:18.167 回答
0
 public class numberOfYears {
    public static void main(String args[]){
    String minsString = JOptionPane.showInputDialog("Enter numbers of minutes:");
    double mins = Double.parseDouble(minsString);
   //calcuate mins in a year
    double minsOfaYear = 365*24*60;
    double year = mins / minsOfaYear;
    double day = (mins % minsOfaYear)/(24*60);
    System.out.println((int)year+ " years and " + (int)day + " days");
    }
}
于 2013-05-01T03:54:54.567 回答
0

我会这样做

    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    c2.add(Calendar.MINUTE, 1000000000);
    int year1 = c1.get(Calendar.YEAR);
    int day1 = c1.get(Calendar.DAY_OF_YEAR);
    int year2 = c2.get(Calendar.YEAR);
    int day2 = c2.get(Calendar.DAY_OF_YEAR);
    int years;
    int days;
    if (day2 > day1) {
        years = year2 - year1;
        days = day2 - day1;
    } else {
        c2.add(Calendar.YEAR, -1);
        years = year2 - year1 - 1;
        days = c2.getActualMaximum(Calendar.DAY_OF_YEAR) - day1 + day2;
    }
    System.out.println(years + " years " + days + " days from now");

输出

1901 years 119 days from now
于 2013-05-01T04:38:29.040 回答