【问题标题】:Unable to type using the Java Scanner无法使用 Java 扫描程序键入
【发布时间】:2013-02-09 04:28:10
【问题描述】:

所以我昨天刚开始自学 Java,在我无限的无聊中决定测试我所知道的一点点,并试图制作一个简单的基于文本的游戏。我正在使用扫描仪询问玩家他想成为哪个班级,但是当我运行代码时,我无法输入任何内容。 到目前为止,这是我的代码

import java.util.Scanner;
public class mainscript {
public static void main(String args[]){
    System.out.println("Welcome to your first quest, young lord.");
    System.out.println("In this quest, you will face many foes.");
    System.out.println("What type of warrior are you, Sire?");
    System.out.println("[use propper caps](Warrior, Rogue, Mage, Brute)");
    Scanner c = new Scanner(System.in);
        Scanner Warrior = null;
        Scanner Rogue = null;
        Scanner Mage = null;
        if (c == Warrior){//c = class
            System.out.println("So you are a warrior.");
            System.out.println("You come equipped with the following items.");
            System.out.println("Sword(20), Shield(10), Gold(20), and 50 points of health.");
            System.out.println("Defeat enemies and do side quests to earn more money for items.");
            int Sword, Shield, Health, Money;
            Sword = 20;
            Shield = 10;
            Health = 50;
            Money = 20;
        }else if(c == Rogue){ 
            System.out.println("So you are a Rogue.");
            System.out.println("You are equipped with the following items.");
            System.out.println("Dagger(20), Gloves(50), Buckler(10), Gold(20) and 40 points of health");
            System.out.println("Defeat enemies and do side quests to earn more money for items.");
            System.out.println("Alternately, use your Gloves to steal gold from your enemies in combat.");
            int Dagger, Gloves, Buckler, Money, Health;
            Dagger = 20;
            Gloves = 50;
            Buckler = 20;
            Money = 20;
            Health = 40;
        }else if(c == Mage){
            System.out.println("So you are a Mage.");
            System.out.println("You are equipped with the following items.");
            System.out.println("");
            System.out.println("Defeat enemies and do side quests to earn more money for items.");
            int Knife, Mana, Health;
        } 

}
}

我只是想知道为什么我在 Eclipse 中运行代码时无法输入任何内容。同样,我对编程非常陌生,所以如果你能相应地回答我可以理解我的错误,我将非常感激。

【问题讨论】:

  • 首先,javascript在哪里?其次,请通过tutorial 进行扫描。这应该可以帮助你。
  • 是的,Java != JavaScript,因为它们是两种完全不同的动物。

标签: java java.util.scanner typing


【解决方案1】:

关于

Scanner c = new Scanner(System.in);
Scanner Warrior = null;
Scanner Rogue = null;
Scanner Mage = null;
if (c == Warrior){
  //....

这不是 Scanner 类的工作方式。您的代码将只需要 一个 Scanner 对象,并且需要调用此对象上的方法,例如 nextLine() 方法,以获取用户的输入。在使用新工具之前,请查看包括 Scanner Tutorial 在内的教程,因为它可以帮助您快速上手。编译器和 JVM 是无情的,不允许你编写代码并祈祷它可以工作。

即,

Scanner scan = new Scanner(System.in);

String userInput = scan.nextLine();
if ("Warrior".equals(userInput)) {
   // do something
}

【讨论】:

    猜你喜欢
    • 2013-08-19
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多