我应如何设置在while循环中决定的变量

时间:2019-01-14 21:41:16

标签: while-loop

我是目前正在制作游戏的新程序员,但是在决定如何允许人们选择角色时遇到困难,每个角色对健康造成的损害等都有不同的值,但是我希望在一段时间内循环使用,所以如果错误键入它只会问一个问题,有什么更好的方法可以使我能够在代码中的任何地方访问变量?

int damage;
int health;
int speed;
int x = 0;
while(x == 0) {
    String role;
    System.out.println("Would you like to select a knight, wizard, or rogue? If you would like more information on class tpye: info(class) with no spaces.");
    role = user_input.next();
    if (role.equalsIgnoreCase("infoknight")) {
        System.out.println("You start with a normal sword. The knight has 15 health, 10 damage, and 5 speed");
    }else if(role.equalsIgnoreCase("infowizard")){
        System.out.println("You start with a normal wand. The wizard has 10 health 10 damage and 10 speed and is the most balanced class to start with");
    }else if(role.equalsIgnoreCase("inforogue")) {
        System.out.println("You start with a normal dagger. The rogue has 10 health, 5 damge, and 15 speed and is the most difficult class to start with\n");
    }else if(role.equalsIgnoreCase("knight")) {
        System.out.println("You selected a knight!\nYou will start with a wooden sword.");
        damage = 10;
        health = 15;
        speed = 5;
        x = 1;
    }else if(role.equalsIgnoreCase("Wizard")) {
        System.out.println("You selected a wizard!\n You will start with a old wand.");
        damage = 10;
        health = 10;
        speed = 10;
        x = 1;
    }else if(role.equalsIgnoreCase("rogue")) {
        System.out.println("You selected a rogue!\nYou will start with a dull dagger");
        damage = 5;
        health = 10;
        speed = 15;
        x = 1;
    }else {
        System.out.println("Incorrectly typed please try again");
    }


}

1 个答案:

答案 0 :(得分:0)

最好为自己设置一个项目来学习编码,所以请意识到这种设计还有路要走。

对于Java或任何其他面向对象的语言,您需要根据程序所操作的对象或实体进行思考。在这种情况下,您有一个对象(即Player的class),并且您还具有许多连接的属性,这些属性可能应该分组为子对象,或者保存在某些对象的集合中排序(例如,地图)。循环break的编程概念也可以使您清理干净。