2016-02-11 30 views
0

编辑:我不能使用的扫描仪。我知道,它更好。正常运行时在NetBeans而不是在命令提示

该程序的目的是接受来自用户的闰秒四位整数,并确定它是否是闰年,通知用户结果,然后提示重置程序以接受另一个条目。如果用户在公元1582年之前输入日期,它将被拒绝。

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!"); 
      } 
       else if(isLeapYear == false && year > 1581){ 
       System.out.println(year + " is not a Leap Year... how unfortunate."); 
      } 
      else{ 
       System.out.println("There are no leap years before 1582!"); 
      } 
    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(); //Clears restart choice from the buffer. 
    year=(int)System.in.read(); //Clears year from the buffer. 

    }    
}//End main 
}//End class 

我到运行的问题是,在命令提示符下,无论第二项是什么,它总是会说其不到1582我有同样的问题,当我在NetBeans开发它,通过使用“year =(int)System.in.read();”清除缓冲区来修复它。该程序在此之后顺利运行,但现在使用命令提示符运行时,问题重复出现。

这是试图运行使用值2916,1900年计划的3次迭代的结果,1432

///// In Netbeans 

Enter target year: 2916 
2916 is a Leap Year! What a time to be alive! 

Reset program? y/n 
y 
Enter target year: 1900 
1900 is not a Leap Year... how unfortunate. 

Reset program? y/n 
y 
Enter target year: 1432 
There are no leap years before 1582! 

Reset program? y/n 
n 
BUILD SUCCESSFUL (total time: 49 seconds) 

///// end of program 

///// In command prompt 

Enter target year: 2916 
2916 is a Leap Year! What a time to be alive1 
Reset program? y/n 
y 
Enter target year: 1900 
There are no leap years before 1582! 
Reset program? y/n 
y 

///// End of program 

有几件事情是错误的。 1.始终认为用户输入的#< 1582上的程序的第二轮。

  • 在NetBeans运行的条目是否是闰年,其中用户必须按一个键以继续到复位提示,对于一些结果之后有位原因命令提示符绕过此。这不一定是问题,事实上它更好,但它可能与问题有关。

  • 程序不管是在复位提示进入第二次迭代之后结束。

  • +0

    你应该考虑使用'Scanner'用户输入。 – FredK

    +0

    我需要不使用扫描仪。 –

    回答

    0

    你能后readCh = (int)System.in.read();

    添加a System.out.println(readCh);我不明白为什么你有行:

    year=(int)System.in.read(); //Clears year from the buffer.

    您可以清除一年一年= 0;

    +0

    当我这样做,我得到这个https://i.gyazo.com/bad3acc90bdeb392c719e814c697acfb.png –

    +0

    他们是一个10字符是谁把作为今年第一个字符1900年的书面方式时(这是一个新行字符)年。因此,第二年应该是190或152年。 试图理解为什么他们在这里是一个新的行字符。 (我怀疑这行:year =(int)System.in.read()) – sab

    +0

    当我这样做时没有任何变化。年份=(int)System.in.read())的原因是因为某些原因,年份= 0会将其保持为0,从而导致相同的错误。 –

    相关问题