我的程序中的计算输出错误但代码似乎是正确的?

时间:2011-10-27 18:35:45

标签: java eclipse

import java.util.Scanner; 
import java.util.InputMismatchException;

public class date {
public static int t1;           //integers for methods
public static int t2;
public static int x = 0;        //integer for looping
public static int y1;
public static int m1;
public static int d1;
public static int y2;
public static int m2;
public static int d2;

public static void date1() {
    do                                                                      //along with "while" statement. Makes loop if error occurs
{   
    Scanner scanner = new Scanner(System.in);
    try {                                                                   // try (run program as normal)
        System.out.println("Please enter the first date ");
        System.out.println("Please enter the year: ");                      //entering date
        y1 = scanner.nextInt();

        System.out.println("Please enter the month: ");
        m1 = scanner.nextInt();

        System.out.println("Please enter the day: ");
        d1 = scanner.nextInt();
        break;                                                              // break loop if input is correct
    } catch (InputMismatchException inputMismatchException) {               // response to "Try". if input is incorrect error will be displayed
        scanner.nextLine();
        System.err.println("You must enter intergers. Please try again. ");
    }
     x = x + 1 ;                                                                // set loop to three attempts
}   while (x < 3) ;                                                         //do process occurs while attempts are under < 4

}

public static void caldate1() {
    do                                                                      //along with "while" statement. Makes loop if error occurs
{   
    int j = 693502;
    try {
        if (m1 == 1 || m1 == 3 || m1 == 5 || m1 == 7 || m1 == 8 || m1 == 10  // if/else statements to set proper numbers of days in each month
                || m1 == 12) {
            t1 = ((365 * y1) + d1 + 31);
        } else if (m1 == 2) {
            t1 = ((365 * y1) + d1 + 28);
        } else if (m1 == 4 || m1 == 6 || m1 == 9 || m1 == 11) {
            t1 = ((365 * y1) + d1 + 30);
        }
        if (t1 >= j)                                                        //if/else statement to catch error if date inputted isnt over jan 1st 1900
        { break;}                                                           // break loop if date is greater or equal to jan 1st 1900
        else {
            System.err.printf("Please enter a date after Jan 1st 1900.\n");
        }
    } catch (InputMismatchException inputMismatchException) {                   //error if date too small
        System.err.println("You must enter intergers. Please try again. ");
    }
     x = x + 1 ;                                                                // set loop to three attempts
    }   while (x < 3) ; 
}

public static void date2() {
    do                                                                      // process to follow if length == 5
{   
    Scanner scanner = new Scanner(System.in);
    try  {                                                                  // try (run program as normal)
        System.out.println("Please enter the second date ");
        System.out.println("Please enter the year: ");                       // entering second date
        y2 = scanner.nextInt();

        System.out.println("Please enter the month: ");
        m2 = scanner.nextInt();

        System.out.println("Please enter the day: ");
        d2 = scanner.nextInt();
          break;                                                                // break loop if input is correct
    } catch (InputMismatchException inputMismatchException) {               //   response to "try". if input is incorrect error will be displayed
        scanner.nextLine();
        System.err.println("You must enter intergers. Please try again. ");
    }
     x = x + 1 ;                                                                // set loop to three attempts
}   while (x < 3)  ;                                                            //do process occurs while attempts are under < 4

}

public static void caldate2() {
     do                                                                 //all code is same as in caldate1()... just second set of variables.
{   
    int j = 693502;
    try {
        if (m2 == 1 || m2 == 3 || m2 == 5 || m2 == 7 || m2 == 8 || m2 == 10
                || m2 == 12) {
            t2 = ((365 * y2) + d2 + 31);
        } else if (m2 == 2) {
            t2 = ((365 * y2) + d2 + 28);
        } else if (m2 == 4 || m2 == 6 || m2 == 9 || m2 == 11) {
            t2 = ((365 * y2) + d2 + 30);
        }
        if (t2 >= j) 
        { break;}
        else {
            System.err.printf("Please enter a date after Jan 1st 1900.\n");
        }
    } catch (InputMismatchException inputMismatchException) {
        System.err.println("You must enter intergers. Please try again. ");
    }
     x = x + 1  ;                                                               // set loop to three attempts
    }   while (x < 3) ; 
}
public static void finaldate1() {
    x = Math.abs(t1 - t2);  //calculate total day difference. output absolute value so that result is always positive
    System.out.println("The difference between the two dates is: " + x  + " days.");    
}

public static void main(String[] args) {

    date1();
    caldate1();
    date2();
    caldate2();
    finaldate1();


}
    }

比如说你输入2011年2月28日和2011年3月1日。 天数应为1.但它计算24.任何人都知道问题是什么?因为当我输入其他日期时,计算日期就好了。

3 个答案:

答案 0 :(得分:1)

呀。不要那样做。创建两个GregorianCalendar类,将两者转换为毫秒并减去以找出差异。然后转换回几天。

确保你看月份,因为GregorianCalendar愚蠢地基于0。

答案 1 :(得分:0)

尝试将此修改为您的算法。我有一系列的int代表每个月的天数。然后,如果您正在查看二月(月== 2),那么您可以将数组中第二个元素之前的所有值添加到总数(t)。我还通过添加参数来重构该方法,因为您的设计目前很丑陋(您必须执行完全相同的功能,但写入两次)。

编辑:我假设月份在输入时被编入索引0。如果不是,则在将其传递给此方法之前从中减去1。

public static int months[] = new int {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    //Note Im assuming error checking has already been done on the data (especially month)
public static int caldate(int year, int month, int day)
{
    int t = 0;
    t += year * 365; // assume no leap years

    for(int i = 0; i < month && i < 12; i++)
    {
       t += months[i];
    }

    t += day;

    return t;
}

答案 2 :(得分:-1)

我建议使用joda时间。它可以让您在没有太多麻烦的情况下对日期进行简单的数学计算,并计算从1到12的月份。

DateMidnight dm1 = new DateMidnight(y1, m1, d1);
DateMidnight dm2 = new DateMidnight(y2, m2, d2);
Days days = Days.daysBetween(dm1, dm2);
int dayCount = days.getDays();