【发布时间】:2014-06-24 05:02:31
【问题描述】:
我需要使用模操作数将输入的天数转换为其等效的年/秒、月/秒、周/秒和天数。我们昨天才开始讨论,所以我还是有点生疏,但这是我制作的程序:
import java.util.Scanner;
public class DaysConvert {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int noOfDays, year, month, week, days;
System.out.print("Enter Number of Days: ");
noOfDays = input.nextInt();
year = noOfDays/365;
month = year%365;
week = month%30;
days = week%7;
System.out.println("Year: " + year);
System.out.println("Month: " + month);
System.out.println("Week: " + week);
System.out.println("Day: " + days);
}
}
我的算法是错的还是什么?
【问题讨论】:
-
这既是“错误”又是“某事”。您是否希望能够仅基于
year计算出month、week和days? -
闰年呢?
-
那么有 31 天的月份呢?还是28?最好使用joda-time。
-
@RobbyCornelissen 即使是 Joda 也做不到。问题定义不够明确。
-
@DavidWallace 同意。需要有一个固定的瞬间。