2011-10-26 74 views
0
import java.util.Scanner; 
import java.util.InputMismatchException; 
public class assignment2 { 
    public static int t1; 
    public static int t2; 
    public static int x; 
    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() { 
     int x = 0; // Integer for looping. 
     do   // Process to follow if length == 5. 
     {   // Notify program what to do while under loop limit. 
      Scanner scanner = new Scanner (System.in); 
      try 
      { 
       System.out.println("Please enter the first date "); 
       System.out.println ("Please enter the year: "); 
       y1=scanner.nextInt(); 

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

       System.out.println("Please enter the day: "); 
       d1=scanner.nextInt();} 
      catch (InputMismatchException inputMismatchException) // Error if wrong character is inputted. 
      { 
       scanner.nextLine() ; 
       System.err.printf("You must enter intergers. Please try again.\n"); // Prompt user to enter integers. 
      } // Set loop to three attempts. 
     } 
     while (x < 3) ; 

     int j = 693502; 
     if (t1 > j) { 
      if (m1==1 + 3 + 5 + 7 + 8 + 10 + 12) { 
       t1 = ((365*y1)+d1+31); 
      } 
      else 
       if (m1==2) { 
        t1 = ((365*y1)+d1+28); 
       } 
       else 
        if (m1==4 + 6 + 9 + 11); { 
         t1 = ((365*y1)+d1+30); 
        } 
      x = x + 1; // Set loop to three attempts 
      while (x < 3) ; 
     } 
     else { 
      System.err.printf ("Error. Please enter a date after Jan 1st 1900.\n") ; 
     } 
    } 

    public static void date2() { 
     int x = 0; // Integer for looping 
     do   // Process to follow if length == 5 
     { 
      Scanner scanner = new Scanner (System.in); 
      try 
      { 
       System.out.println("Please enter the first date "); 
       System.out.println ("Please enter the year: "); 
       y2=scanner.nextInt(); 

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

       System.out.println("Please enter the day: "); 
       d2=scanner.nextInt();} 
      catch (InputMismatchException inputMismatchException) // Error if wrong character is inputted. 
      { 
       scanner.nextLine() ; 
       System.err.printf("You must enter intergers. Please try again. "); // Prompt user to enter integers. 
      } 
      x = x + 1; // Set loop to three attempts 
     } 
     while (x < 3) ; 
     int j = 693502; 
     if (t1 > j) { 
      if (m1==1 + 3 + 5 + 7 + 8 + 10 + 12) { 
       t1 = ((365*y1)+d1+31); 
      } 
      else 
       if (m1==2) { 
        t1 = ((365*y1)+d1+28); 
       } 
       else 
        if (m1==4 + 6 + 9 + 11); { 
         t1 = ((365*y1)+d1+30); 
        } 
      x = x + 1; // Set loop to three attempts. 
      while (x < 3) ; 
     } 
     else { 
      System.err.printf ("Error. Please enter a date after Jan 1st 1900. "); 
     } 
    } 

    public static void finaldate1() { 
     x = Math.abs(t1-t2); 
    } 

    public static void main(String[] args) { 
     date1(); 
     date2(); 
     finaldate1(); 
     System.out.println("The difference between the two dates is: " + x + " days."); 
    } 
} 

我的循環沒有正確響應的原因是什麼?我怎樣才能設置一個適當的錯誤捕獲和循環,當非整數輸入或如果輸入日期的總天數小於693502(1990年1月1日)。這個程序爲什麼會立即循環,而不是當它應該是什麼時候?

+3

我完全看不懂。 –

+1

如果這是家庭作業,您應該將其標記爲家庭作業。而且我真的希望這不是你想要的,因爲我根本看不懂代碼在做什麼。 –

+0

這更好:) –

回答

1

您應該在同時線的末尾刪除分號和sorround要在括號中的循環包括的區域,即{和}

0

你的循環條件是錯誤的。例如,在date1輸入循環中,您有while (x < 3);但在該循環內從不修改x

0

首先,看看錯誤並讓它編譯。

  1. while循環的一半是獨立的,沒有做任何事情。
  2. 所有while循環都引用一個未設置的x,
  3. 此外,您正在使用局部變量重寫類級別x變量,該變量當前不是錯誤,但很可能會導致。

一般調試通知,將問題分解成小塊,並確認小塊工作。在開始下一部分之前保存工作(最好是版本控制),以便始終可以返回到工作代碼。

相關問題