-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 オブジェクトは1 つだけで、メソッドなど、このオブジェクトのメソッドを呼び出してnextLine()、ユーザーからの入力を取得する必要があります。新しいツールを使用する前に、スキャナーチュートリアルを含むチュートリアルを参照してください。コンパイラと JVM は容赦なく、コードを作成してそれが機能することを祈ることを許可しません。

つまり、

Scanner scan = new Scanner(System.in);

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