这是我的代码:
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天的结果..但我没有得到正确的结果。有人可以帮我指出问题吗?谢谢!
答案 0 :(得分: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;
}
答案 1 :(得分: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");
}
}
答案 2 :(得分: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");
}
}
答案 3 :(得分: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