我的學校任務是用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();
}
}
}
爲什麼要計算任何事情,如果目標年份是'1580'例如? – ChiefTwoPencils
問題是你忘記在'restartChoice'後面讀取換行符 - 一個字符值爲13。這被認爲是今年的第一位,將年份初始化爲「-35000」。打印年份,你會看到。 – Kenney
不知道爲什麼這個關閉。問題是可重複的,錯誤不是「簡單的錯字」,而是一個邏輯錯誤。 – RealSkeptic