Java闰年if-else语句卡住

时间:2016-02-04 00:31:49

标签: java

我上学的任务是在java中编写一个程序,接受来自System.in.read()的四位数条目;并计算它是否是闰年,然后告诉用户它是否是闰年,然后提供重启程序的选项。此外,如果用户在1582年之前输入一年,它将告诉他们错误,然后是程序重置选项。

在我继续之前,我意识到有更好的方法来处理文本输入,事实上我使用扫描仪为此编写了一个完整的程序,但是由于我们不会在下周直到下周覆盖它,老师说我不能用它。

这是我的老师为处理文本输入而给出的代码。

     // Program using read() method to read 4 consecutive characters and convert to a year
 // JAK

 class ReadLeapYear {

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

            char restartChoice;
            int readCh, year=0, i;

            do
            {

            System.out.print("Enter the year: ");
            for(i = 0; i < 4; i++)
            {
            readCh =  (int)System.in.read();
            switch (i)
            {
                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

            System.out.println(year);
           readCh = System.in.read(); // Clear the carriage return in the buffer
           readCh = System.in.read(); // Clear the linefeed in the buffer

            year = 0; // Reset year for another choice
            System.out.print("Do you wish to test another year? Y/N \n");
            restartChoice=(char)System.in.read();
            }while(restartChoice == 'Y');
        }                                                                       //end main
}                                                                               //end class

以下是我到目前为止编写的代码,根据我以前的工作逻辑,据我所知,该代码应该可以正常工作。

package revisedleapyear;


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(year < 1582){
                System.out.println("There are no leap years before 1582! \nPress Enter to continue.");
            }
            else 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.");
            }
    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年开始给出的任何数字都是闰年。第二个问题是,如果我选择一个低于1582的数字,然后重新启动该程序,它将总是表现得好像我选择了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: 2001 
> There are no leap years before 1582!  
> Press Enter to continue. 
> Reset program? y/n
编辑:我已经解决了这个问题,它只是将事情宣布为闰年。但现在无论如何,如果我重新启动程序,第二个输出将总是说我选择了1582之前的日期,即使我没有。

0 个答案:

没有答案