java - 当我执行程序时,程序显示 java.util.NoSuchElementException : No line found

标签 java compiler-errors java.util.scanner

我对java有点陌生,我想知道我的代码中做错了什么。问题出在“System.out.println(pw.next());”该代码适用于类似口袋妖怪的游戏,扫描仪应该扫描该人的用户名。我还远远没有完成代码,而且布局有点奇怪,因为我尝试自己修复错误。

如果有人能提供创建有趣游戏的技巧,我们将不胜感激。

package test;
import java.lang.Math;
import java.util.*;

public class Pokemon {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Welcome to Daniel's Game");
        System.out.println("\nFire. Water. Earth. Air. The four nations lived in harmony until the Avatar attacked. \nFueled by anger, this avatar, whose name remains unknown, has sought to take over the world.\nLegends say that he lives deep in the Himalayas and wields the power of all four elements, but nobody can say for sure");
        System.out.println("It is your job to save the world from catastrophe");
        System.out.println("Are you up for the Challenge?");
        String y = sc.next();
        if(y.equals("yes")||y.equals("Yes")) {
            System.out.println("You better be");
        } else {
            System.out.println("The world ends cuz you suck");
            System.exit(0);
        }
        sc.close();
        name();

    }
    public static void name() {
        System.out.println("What is Your Name?");
        Scanner pw = new Scanner(System.in);
        String o = pw.next();
        pw.close();
        Scanner ew = new Scanner(System.in);
        System.out.println("\nChoose a Pokemon: \n1. Fire \n2. Water\n 3. Earth \n4. Air");

        int x = ew.nextInt();

        ew.close();
    }

}

最佳答案

下面应该有效:

package test;
import java.lang.Math;
import java.util.*;
public class Pokemon {

    private static Scanner sc;

    public static void main(String[] args) {
         sc = new Scanner(System.in);
        System.out.println("Welcome to Daniel's Game");
        System.out.println("\nFire. Water. Earth. Air. The four nations lived in harmony until the Avatar attacked. \nFueled by anger, this avatar, whose name remains unknown, has sought to take over the world.\nLegends say that he lives deep in the Himalayas and wields the power of all four elements, but nobody can say for sure");
        System.out.println("It is your job to save the world from catastrophe");
        System.out.println("Are you up for the Challenge?");
        String y = sc.next();
        if(y.equals("yes")||y.equals("Yes")) {
            System.out.println("You better be");
        } else {
            System.out.println("The world ends cuz you suck");
            System.exit(0);
        }
       // sc.close();
        name();

    }
    public static void name() {
        System.out.println("What is Your Name?");
        String name = sc.next();
       // Scanner pw = new Scanner(System.in);
       // String o = pw.next();
      //  pw.close();
        //Scanner ew = new Scanner(System.in);
        System.out.println("\nChoose a Pokemon: \n1. Fire \n2. Water\n 3. Earth \n4. Air");

        int x = sc.nextInt();

        sc.close();
    }

}

关于java - 当我执行程序时,程序显示 java.util.NoSuchElementException : No line found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61420161/

相关文章:

Java 与 Sublime Text - 扫描仪无法工作?

java - 我的手电筒应用程序出现错误

WordPress "Warning: Missing argument 2"错误

python - cythoning 类时出现空声明符错误?

java - 扫描仪 NoSuchElementException

Java从带有撇号和连字符的文件中读取单词并将它们计为2个单词

java - 使用来自现有列表的 HashMap 的值创建一个新列表

java - JSP中如何引用css、images、java script等静态资源?

java - 获取 FontMetrics StackOverflowError

c++ - 奇怪的错误: label 'loopend' used but not defined, expected unqualified-id before “if ” (same error for “goto” and “' }' token ”