使用扫描仪从同一类的两个对象获取输入

时间:2018-08-13 17:00:59

标签: java.util.scanner

我目前正在编写的简单程序的功能存在问题。我正在尝试创建两个对象,每个对象都要求用户提供一个名称,然后使用Scanner从几个选项中进行选择。第一个对象,类createMonster的怪物,正在要求用户通过Scanner提供信息。但是,在创建类monster2的第二个对象createMonster时,程序不会要求用户输入。

我需要在班级CreateScanner上进行一些更改吗?还是更大的问题?

public class MainClass {

    public static void main(String[] args) {
        RandomMonsterGenerator monster = new RandomMonsterGenerator();
        monster.createMonster();
        RandomMonsterGenerator monster2 = new RandomMonsterGenerator();
        monster2.createMonster();
    }
}

RandomMonsterGenerator代码:

public class RandomMonsterGenerator {

    // Objects
    Attributes attr = new Attributes();
    CreateScanner createScanner = new CreateScanner();

    // Variables
    String monsterName;
    String attributesValues;
    int choice;

    // Main method for generating monster
    public void createMonster() {
        attr.generateAttributes();
        generateName();
        chooseClass();
        System.out.println("Generating random stats:");
        attributesValues = attr.toString();
        System.out.println(attributesValues);
        createScanner.closeScanner();
    }

    // Generating monster name
    private void generateName() {
        System.out.println("Name your monster: ");
        monsterName = createScanner.stringInput();
        System.out.println("Name of the monster: " + monsterName);
    }

    // Choosing a class
    private void chooseClass() {
        System.out.println("Class descriptions: ");
        System.out.println("Warrior has +2 to Strength and +2 to Condititon.");
        System.out.println("Thief has +2 to Dexterity and +2 to Charisma.");
        System.out.println("Mage has +2 to Intelligence and +2 to Wisdom.");
        System.out.println("**************************************************");
        System.out.println("Choose your class from following options: ");
        System.out.println("Warrior, press '1'");
        System.out.println("Thief, press '2'");
        System.out.println("Mage, press '3'");
        choice = createScanner.intInput();
        switch(choice) {
            case 1:
                Warrior warrior = new Warrior(attr);
                System.out.println(monsterName + " is a warrior.");
                break;
            case 2:
                Thief thief = new Thief(attr);
                System.out.println(monsterName + " is a thief.");
                break;
            case 3:
                Mage mage = new Mage(attr);
                System.out.println(monsterName + " is a mage.");
                break;
            default:
                System.out.println("No option choosen.");
                break;
        }
    }
}

CreateScanner代码:

import java.util.Scanner;

public class CreateScanner {

    Scanner sc = new Scanner(System.in);

    public String stringInput() {
        String input = "";
        if (sc.hasNextLine()) {
            input = sc.nextLine();
        }
        return input;
    }
    public int intInput() {
        int input2 = 0;
        if (sc.hasNextLine()) {
            input2 = sc.nextInt();
        }
        return input2;
    }
    public void closeScanner() {
        sc.close();
    }
}

1 个答案:

答案 0 :(得分:0)

两件事。

首先,在完成扫描仪操作之前,请不要关闭扫描仪。这将关闭System.in  以及一旦您这样做,您将不再获得任何输入。这就是为什么它仅跳过第二个RandomMonsterGenerator输入的原因。

第二,仅创建一个Scanner并将其作为参数传递给RandomMonsterGenerator。这使事情变得简单。

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    RandomMonsterGenerator monster = new RandomMonsterGenerator(scanner);
    RandomMonsterGenerator monster2 = new RandomMonsterGenerator(scanner);
}