Netbeans- NoSuchElementException

时间:2019-06-08 21:20:06

标签: java nosuchelementexception

代码在Gitbash命令行(GitHub)中正确运行。但是,当我在NetBeans中编译并运行它时,它给了我NoSuchElement Exception。如果有人可以帮助我使用NetBeans,我将不胜感激。

     package MyApp;

     import java.util.Scanner;

     public class MyApp {
     /** 
     * Starts the program 
     * 
     * @param args command line arguments 
     */
     public static void main(String[] args) {
        // welcome the user to MyApp
        System.out.println("Welcome to MyApp");
        System.out.println();  // print a blank line

        // create a Scanner object named sc
        Scanner sc = new Scanner(System.in);

        // check if the number you enter is even or odd until choice isn't
        equal to "y" or "Y"
        String choice = "y";
        while (choice.equalsIgnoreCase("y")) {
            // get the integer from the user
            System.out.print("Enter an integer:   ");
            double integer = sc.nextDouble();

            // check whether it is even or odd
            if (integer % 2 == 0) {
              System.out.println("It is an even integer");
            } else {
                System.out.println("It is an odd integer");  ;
            }

            // see if the user wants to continue
            System.out.print("Continue? (y/n): ");
            choice = sc.next();
            System.out.println(); 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我运行了该代码,它运行完美!我唯一更改的是在线

    //check if number you entered is even or odd until choice isnt 
    equal to "y" or "Y"

我将其更改为:

    //check if number you entered is even or odd until choice isnt 
    //equal to "y" or "Y"

尽管有时扫描程序会卡在上一件事上,所以您可以尝试使用sc.nextLine();,以便它将转到下一行并采用新的行字符。