有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何修复NoTouchElementException扫描程序?

所以Scanner抛出NoSuchElementException找不到任何行,就像它没有等待我的输入一样,请注意,我只调用nextLine()我不调用nextInt()。我只打了一次电话nextLine()

这是我的全部代码

public class MyClass{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println(scanner.nextLine());
    }
}

这是我得到的错误

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at com.ekisache.games.MyClass.main(MyClass.java:9)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':Test:MyClass.main()'.
> Process 'command 'C:/Program Files/Android/Android Studio/jre/bin/java.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

这和我的另一台电脑一样很奇怪,这台电脑工作正常,而这台电脑却没有

我的mac中的工作代码:

enter image description here

在我的windows PC中:

enter image description here


共 (1) 个答案

  1. # 1 楼答案

    试着先分配一个变量

    String yourInput = scanner.nextLine();
    System.out.printLn(yourInput)