设置难度不起作用

时间:2015-07-19 01:23:20

标签: java java.util.scanner

我正在eclipse luna中制作一个简单的java文本游戏。 Scanner.next()不起作用。当我运行它时,在我请求用户输入后,会显示错误(java.util.NoSuchElementException)。我刚刚做了一些非常愚蠢的事吗?

    while(whle) {
        System.out.print("Where do you want to go? Enter gym, store, or mountain. ");
        loc = goTo.next();
        loc = loc.toLowerCase();
        if(loc.equals("gym") || loc.equals("store") || loc.equals("mountain")) whle = false;
        else {
            System.out.println("Please enter an option. ");
            System.out.println();
        }
    }

所有内容都已定义,并且在我运行它时,实际程序中不会弹出任何错误。

1 个答案:

答案 0 :(得分:1)

确保您的扫描仪已正确初始化

Scanner loc = new Scanner(System.in); //very important

您已导入此课程

import java.util.Scanner; //in the begining of the source code

它应该工作(除了我认为你的IDE,java版本的问题)

如果这没有帮助,我自己用这个程序运行它(使用boolean whle初始化,main方法等等,它产生了我认为你想要的东西。

while(whle) {
    System.out.print("Where do you want to go? Enter gym, store, or mountain. ");
    String locstring= loc.next();
    if(locstring.equalsIgnoreCase("gym") || locstring.equalsIgnoreCase("store") || locstring.equalsIgnoreCase("mountain")){ whle = false;}
    else {
        System.out.println("Please enter an option. ");
        System.out.println();
    }
}
相关问题