Java Scanner需要多个条目

时间:2015-06-24 21:32:23

标签: java java.util.scanner

以下是我用Java编写的用于在游戏中设置播放器的一些代码。我为atk,def和hp设置的扫描仪需要2-3个条目。我为凌乱的代码道歉,我刚开始学习Java。

String NAME;
int ATK;
int DEF;
int HP;
int remainingPoints = 10;
// pick name
System.out.println("Enter name:");
Scanner p2Name = new Scanner(System.in);
NAME = p2Name.nextLine();
// pick type
System.out.println("Choose element theme: (water,fire,ice)");
Scanner p2Type = new Scanner(System.in);
String TYPE = p2Type.nextLine();
// atk
System.out.println("You have ten points to disperse " + "among your atk, def and hp");
System.out.println("Enter atk:");
Scanner p2Atk = new Scanner(System.in);
if (p2Atk.nextInt() <= 10) {
    ATK = p2Atk.nextInt();
    remainingPoints = remainingPoints - ATK;
} else {
    System.out.println("Your entry was invalid, your " + "atk is now five by default.");
    ATK = 5;
    remainingPoints = remainingPoints - ATK;
}
// def
System.out.println("You now have " + remainingPoints + ". Please enter def:");
Scanner p2Def = new Scanner(System.in);
if (p2Def.nextInt() <= remainingPoints) {
    remainingPoints = remainingPoints - p2Def.nextInt();
    DEF = p2Def.nextInt();
} else {
    System.out.println("Your entry was invalid, your " + "def is now one by default.");
    DEF = 1;
    remainingPoints = remainingPoints - DEF;
}
// hp
System.out.println("You now have " + remainingPoints + ". Please enter hp:");
Scanner p2Hp = new Scanner(System.in);
if (p2Hp.nextInt() <= remainingPoints) {
    remainingPoints = remainingPoints - p2Hp.nextInt();
    HP = p2Hp.nextInt();
} else {
    System.out.println("Your entry was invalid, your " + "hp is now one by default.");
    HP = 1;
    remainingPoints = remainingPoints - HP;
}
Player p2 = new Player(NAME, TYPE, ATK, DEF, HP);
System.out.println(p2.n);
System.out.println(p2.type);
System.out.println(p2.atk);
System.out.println(p2.def);
System.out.println(p2.hp); 

2 个答案:

答案 0 :(得分:2)

每次提示输入时都无需创建新的Scanner对象。

您要提示输入的数据类型,无论是intdouble还是String,您只需创建一次扫描程序对象 - 即:system.in

但是,使用scn.nextInt()时,Java中的初学者可能会遇到问题。 在提示解决该问题后,您可以放置​​scn.nextLine()

但我个人更喜欢使用Stringscn.nextLine()收到所有输入。如果是int输入,只需执行:

int num = Integer.parseInt(scn.nextLine());

示例:

Scanner scn = new Scanner(System.in);

String name = scn.nextLine();
String type = scn.nextLine();
String spell = scn.nextLine();
int health = Integer.parseInt(scn.nextLine());

答案 1 :(得分:1)

您创建了太多不正确的Scanner对象。

关于要求2-3个条目 - 你应该将值放在一个临时变量中,然后多次使用它。

PFB更正的代码:

String NAME = null;
String TYPE = null;
int ATK = 0;
int DEF = 0;
int HP = 0;
int remainingPoints = 10;

try {

    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter name:");
    NAME = scanner.nextLine();

    System.out.println("Choose element theme: (water,fire,ice)");
    TYPE = scanner.nextLine();

    System.out.println("You have ten points to disperse "
            + "among your atk, def and hp");

    System.out.println("Enter atk:");
    int temp = scanner.nextInt();

    if (temp <= 10)
        ATK = temp;
    else {
        System.out.println("Your entry was invalid, your "
                + "atk is now five by default.");
        ATK = 5;
    }

    remainingPoints = remainingPoints - ATK;
    System.out.println("You now have " + remainingPoints
            + ". Please enter def:");           

    temp = scanner.nextInt();
    if (temp <= remainingPoints)
        DEF = temp;
    else {
        System.out.println("Your entry was invalid, your "
                + "def is now one by default.");
        DEF = 1;
    }
    remainingPoints = remainingPoints - DEF;
    System.out.println("You now have " + remainingPoints
            + ". Please enter hp:");

    temp = scanner.nextInt();
    if (temp <= remainingPoints)
        HP = temp;
    else {
        System.out.println("Your entry was invalid, your "
                + "hp is now one by default.");
        HP = 1;
    }
    remainingPoints = remainingPoints - HP;

    scanner.close();
} catch (Exception e) {
    e.printStackTrace();
}

Player p2 = new Player(NAME, TYPE, ATK, DEF, HP);

System.out.println(p2.n);
System.out.println(p2.type);
System.out.println(p2.atk);
System.out.println(p2.def);
System.out.println(p2.hp);