-1

所以我昨天刚开始自学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 中运行代码时无法输入任何内容。同样,我对编程非常陌生,所以如果你能相应地回答我可以理解我的错误,我将非常感激。

4

1 回答 1

0

关于

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

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

IE,

Scanner scan = new Scanner(System.in);

String userInput = scan.nextLine();
if ("Warrior".equals(userInput)) {
   // do something
}
于 2013-02-09T04:38:46.610 回答