gpt4 book ai didi

java - Netbeans-NoSuchElementException

转载 作者:行者123 更新时间:2023-12-02 04:42:15 31 4
gpt4 key购买 nike

代码在 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();
}
}
}

最佳答案

我运行了该代码,它运行得很好!我唯一改变的是线路

    //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(); 这样它就会转到下一行并获取新行字符。

关于java - Netbeans-NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56510226/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com