Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在netbeans中正常运行,但不在命令提示符下运行_Java - Fatal编程技术网

Java 在netbeans中正常运行,但不在命令提示符下运行

Java 在netbeans中正常运行,但不在命令提示符下运行,java,Java,编辑:我不允许使用扫描仪。我知道,这样更好 此程序的目的是从用户处接受一个闰年四位数整数,并确定它是否为闰年,将结果通知用户,然后提示重置程序以接受另一个条目。如果用户输入的日期早于公元1582年,则该日期将被拒绝 public class RevisedLeapYear { public static void main(String args[]) throws java.io.IOException { char restartChoice = 'y';

编辑:我不允许使用扫描仪。我知道,这样更好

此程序的目的是从用户处接受一个闰年四位数整数,并确定它是否为闰年,将结果通知用户,然后提示重置程序以接受另一个条目。如果用户输入的日期早于公元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
有几件事不对。 1.它总是认为用户在程序第二次运行时输入了#<1582

  • 在netbeans运行中,在条目是否为闰年的结果之后有空格,用户必须按一个键才能继续执行重置提示,出于某种原因,命令提示会被此项绕过。这不一定是个问题,事实上它更好,但它可能与问题有关

  • 无论重置提示中输入了什么,程序在第二次迭代后结束


  • 你能添加
    一个System.out.println(readCh)
    after
    readCh=(int)System.in.read()

    我不明白你为什么要说这句话:

    year=(int)System.in.read()//从缓冲区中清除年份。


    您可以使用年=0清除年

    你应该考虑使用<代码>扫描器< /代码>供用户输入。我不需要使用扫描仪。当我这样做时,我得到的是一个10字符,在1900年的写作过程中被放在一年中的第一个字符(它是一个新的线字符)。因此,第二年应该是190或152左右。试着理解为什么他们在这里是一个新行字符。(我怀疑行:year=(int)System.in.read())在我这样做时没有任何变化。之所以存在year=(int)System.in.read())是因为出于某种原因,执行year=0会将其固定为0,从而导致相同的错误。新理论:在运行中,重置后,不要使用enter键通过这一行。使用任意字母键(在某些环境中,新行不是一个字符,而是两个字符)。
    ///// 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