当我执行程序时,程序显示java.util.NoSuchElementException:找不到行

问题描述 投票:0回答:1

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

如果有人有创建有趣游戏的提示,也将不胜感激。

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

}
java compiler-errors java.util.scanner
1个回答
0
投票

下面应该起作用:

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

}
© www.soinside.com 2019 - 2024. All rights reserved.