2016-02-04 189 views
-1

我的學校任務是用java編寫一個程序,它接受來自System.in.read()的一個四位數字的條目;並計算它是否是閏年,然後告訴用戶它是否是閏年,然後提出重新啓動程序的選項。另外,如果用戶在1582年之前輸入一年,它會告訴他們錯誤,然後是程序重置選項。Java忽略if-else語句

public class RevisedLeapYear { 
    public static void main(String args[]) 
    throws java.io.IOException { 

    char restartChoice = 'y'; 
    int readCh, year=0, i; 
    boolean isLeapYear; 

    while(restartChoice == 'y' || restartChoice == 'Y'){ 
     System.out.print("Enter target year: "); 
     for(i = 0; i < 4; i++)//start for 
     { 
      readCh = (int)System.in.read(); 
      switch(i) //start switch 
      {//converts in to 4 digits 
       case 0: year = (int)((readCh - 48) * 1000); break; 
       case 1: year = year + (int) ((readCh - 48) * 100); break; 
       case 2: year = year + (int) ((readCh - 48) * 10); break; 
       case 3: year = year + (int) (readCh - 48); 
      }//end switch 
    }//end for 
     isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)); 
       if(isLeapYear == true && year > 1581){ 
       System.out.println(year + " is a Leap Year! What a time to be alive! \nPress Enter to continue."); 
      } 
       else if(isLeapYear == false && year > 1581){ 
       System.out.println(year + " is not a Leap Year... how unfortunate. \nPress Enter to continue."); 
      } 
      else{ 
       System.out.println("There are no leap years before 1582! \nPress Enter to continue."); 
      } 
    readCh = System.in.read(); // Clear the carriage return in the buffer 
    readCh = System.in.read(); // Clear the linefeed in the buffer 

    System.out.print("Reset program? y/n \n"); 
    restartChoice=(char)System.in.read(); 

    }    
} 
} 

但由於某些原因,當我重新啓動該程序,然後輸入一個年份,它會說,它1582之前不管是什麼,並顯示所有線路上,直到重置提示沒有停止。

Enter target year: 2004 
2004 is a Leap Year! What a time to be alive! 
Press Enter to continue. 

Reset program? y/n 
y 
Enter target year: 2003 
There are no leap years before 1582! 
Press Enter to continue. 
Reset program? y/n 
n 
BUILD SUCCESSFUL (total time: 21 seconds) 

編輯:修復它我自己。問題是,一年沒有從緩衝區中清除。我將這添加到最後一行。

year=(int)System.in.read(); 

將此作爲最終產品。

public class RevisedLeapYear { 
    public static void main(String args[]) 
    throws java.io.IOException { 

    char restartChoice = 'y'; 
    int readCh, year=0, i; 
    boolean isLeapYear; 

    while(restartChoice == 'y' || restartChoice == 'Y'){ 
     System.out.print("Enter target year: "); 
     for(i = 0; i < 4; i++)//start for 
     { 
      readCh = (int)System.in.read(); 
      switch(i) //start switch 
      {//converts in to 4 digits 
       case 0: year = (int)((readCh - 48) * 1000); break; 
       case 1: year = year + (int) ((readCh - 48) * 100); break; 
       case 2: year = year + (int) ((readCh - 48) * 10); break; 
       case 3: year = year + (int) (readCh - 48); 
      }//end switch 
    }//end for 
     isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)); 
       if(isLeapYear == true && year > 1581){ 
       System.out.println(year + " is a Leap Year! What a time to be alive! \nPress Enter to continue."); 
      } 
       else if(isLeapYear == false && year > 1581){ 
       System.out.println(year + " is not a Leap Year... how unfortunate. \nPress Enter to continue."); 
      } 
      else{ 
       System.out.println("There are no leap years before 1582! \nPress Enter to continue."); 
      } 
    readCh = System.in.read(); // Clear the carriage return in the buffer 
    readCh = System.in.read(); // Clear the linefeed in the buffer 

    System.out.print("Reset program? y/n \n"); 
    restartChoice=(char)System.in.read(); 
    year=(int)System.in.read(); 

    }    
} 
} 
+0

爲什麼要計算任何事情,如果目標年份是'1580'例如? – ChiefTwoPencils

+0

問題是你忘記在'restartChoice'後面讀取換行符 - 一個字符值爲13。這被認爲是今年的第一位,將年份初始化爲「-35000」。打印年份,你會看到。 – Kenney

+0

不知道爲什麼這個關閉。問題是可重複的,錯誤不是「簡單的錯字」,而是一個邏輯錯誤。 – RealSkeptic

回答

1

我重新整理了一下你的代碼,並用掃描儀替換System.in.read()。之後一切正常。問題在於您的System.in.read()聲明。

public static void main(String[] args) 
{ 
    char restartChoice = 'y'; 
    int year=0; 
    boolean isLeapYear; 
    Scanner scn = new Scanner(System.in); 

    while(restartChoice == 'y' || restartChoice == 'Y'){ 
     System.out.print("Enter target year: "); 
     year = Integer.parseInt(scn.nextLine()); 

     isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)); 

     if(isLeapYear && year > 1581) 
      System.out.println(year + " is a Leap Year! What a time to be alive! \nPress Enter to continue.");  
     else if(!isLeapYear && year > 1581) 
      System.out.println(year + " is not a Leap Year... how unfortunate. \nPress Enter to continue.");    
     else 
      System.out.println("There are no leap years before 1582! \nPress Enter to continue."); 

     System.out.print("Reset program? y/n \n"); 
     restartChoice = scn.nextLine().charAt(0); 
    }    
} 
0

問題不在於你是否else語句,而是與如何System.in.read()正在讀它在第二個時間,因爲它不是在第二次閱讀。

你可能想看看如何使用掃描儀。

1

你的問題是處理行尾。

您忘記了每個輸入的用戶輸入後跟一個行尾。這可以只是回車,或回車和換行。

您的程序以四個字節讀取。當用戶輸入這些字節時,他也會按返回。將一個字節插入到流中。

因爲您告訴用戶「按Enter鍵繼續」,用戶再按返回。這會將另一個字節插入到流中。

然後清除流中的兩個字節。你假設你刪除了回車和換行符。但事實上,你只是刪除了一年後的回車,而另一個則被按下。

現在你問用戶yn。但要輸入這些內容,用戶同時點擊返回。所以他輸入兩個個字符到流中。 restartChoice只能得到其中之一 - yn,但回車仍在輸入流中。然後,您返回到循環的頭部,打印提示並啓動for循環。在這一點上,你讀回車。只有這樣你纔會開始閱讀下一年。

所以,如果你明年字符串是2003,你在你的循環實際上得到的是CR 。您的年度計算變爲(13-48)*1000 + (50-48)*100 + (48-48)*10 + (48-48) - 這是一個非常小的數字,因爲第一項是負數。

所以它會告訴你你輸入的年份是1580年之前

  • 始終使用Reader閱讀的文本。不要嘗試讀取原始字節並自行轉換它們。首先,你不知道你的機器是否只會在每一行的末尾輸入一個返回值或是一個返回值和一個換行符。當你更精通編程時,你可以嘗試爲這些情況編寫一個輸入處理程序。但這並不是真的必要 - 你已經有了各種各樣的Reader,或者Scanner這對初學者來說非常好。
  • 當你想解釋一個號碼時,一定要確保它是由有效數字組成的。
  • 還有一個壞邏輯的問題。你不應該在裏面使用forswitch。既然你已經在開關分開來寫每個操作,你還不如完全消除迴路和檢查字符直上:

    System.out.print("Enter target year: "); 
    readCh = (int) System.in.read(); 
    year = (int) ((readCh - 48) * 1000); 
    readCh = (int) System.in.read(); 
    year = year + (int) ((readCh - 48) * 100); 
    readCh = (int) System.in.read(); 
    year = year + (int) ((readCh - 48) * 10); 
    readCh = (int) System.in.read(); 
    year = year + (int) (readCh - 48); 
    

    這不是簡單的比一個開關在一個循環?

  • 而且你應該首先檢查一年是否大於1581,並且只有在它是的時候,檢查它是否跳躍。
+0

我會說這是一個非常詳細的觀察和解釋。 – user3437460