简体   繁体   English

如何将键盘按下转换为 Java 程序中的值?

[英]How can I translate a keyboard press to a value in my Java program?

In a text adventure game written in Java, I want the character race to be as follows( the number corresponds to the key the user types):在Java编写的文字冒险游戏中,我希望人物赛跑如下(数字对应用户键入的键):

   // Character Race
    
    // 1) Human
    // 2) Dwarf
    // 3) Elf
    // 4) Orc

How do I write that out to the player?我怎么把它写给播放器?

Like this?像这样? So I ask the user which race they want:所以我问用户他们想要哪个种族:

    System.out.print("Enter character race: ");
    charRace = input.nextInt();
    System.out.println("Your character's race is " + charRace);

How do I tell my program that 1 = Human, 2 = Dwarf, etc...??我如何告诉我的程序 1 = 人类,2 = 矮人,等等...??

Do I need to create a dictionary or something similar?我需要创建字典或类似的东西吗?

Thanks!谢谢!

You could just use some if statements to identify the entered race.您可以只使用一些 if 语句来识别输入的比赛。 Something like this像这样的东西

if(charRace == 1){
            String race = "Human";
        } else if(charRace == 2){
            String race = "Dwarf";
        } else if(charRace == 3){
            String race = "Elf";
        } else{
            String race = "Orc";
        }

Maybe not the most efficient way to do this though.不过,这可能不是最有效的方法。

This might be a little out of the range of scope, but for me, when you have a limited range of possible values, I'd tend to look towards using a enum这可能有点超出 scope 的范围,但对我来说,当您的可能值范围有限时,我倾向于使用enum

enum Race {
    HUMAN(1), DWARF(2), ELF(3), ORC(4);
    
    private int value;

    private Race(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
    
    public static Race from(int value) {
        for (Race race : Race.values()) {
            if (race.getValue() == value) {
                return race;
            }
        }
        return null;
    }
    
}

Then you can do fun things like...然后你可以做一些有趣的事情,比如......

Race race = Race.from(1);
if (race == null) {
    System.out.println("Invalid selection");
} else {
    switch (race) {
        case DWARF:
            System.out.println("Gunghrim Dwarf!");
            break;
        case ELF:
            System.out.println("Welcome Elf!");
            break;
        case HUMAN:
            System.out.println("Welcome Human!");
            break;
        case ORC:
            System.out.println("Welcome Orc!");
            break;
    }
}

You could even automate the menu.你甚至可以自动化菜单。 Start by adding something like...首先添加类似...

public String getProperName() {
    StringBuilder sb = new StringBuilder(32);
    String name = name();
    sb.append(name.charAt(0));
    sb.append(name.substring(1).toLowerCase());
    return sb.toString();
}

to the Race enum and then you could create a menu doing something like...Race枚举,然后您可以创建一个菜单,执行类似...

List<Race> races = Arrays.asList(Race.values());
races.sort(new Comparator<Race>() {
    @Override
    public int compare(Race o1, Race o2) {
        return o1.getValue() - o2.getValue();
    }
});
for (Race race : races) {
    System.out.println(race.getValue() + ") " + race.getProperName());
}

which prints哪个打印

1) Human
2) Dwarf
3) Elf
4) Orc

Have a look at the enums type trail for more details查看 枚举类型跟踪以获取更多详细信息

If you are just using integers wouldn't a simple Array be the better choice?如果您只是使用整数,那么简单的数组不是更好的选择吗?

String races[] = {"Dwarf","Elf","Human","Orc"};
System.out.println("You selected: "+races[charRace]);

edit:编辑:
If you want error handling:如果你想要错误处理:

String races[] = {"Dwarf","Elf","Human","Orc"};
if(charRace>=0&&charRace<races.length)
    System.out.println("You selected: "+races[charRace]);
else
    System.out.println("Error: invalid input!");

Also it is definetely more efficient to use a switch case over an if-else!此外,在 if-else 上使用 switch case 肯定更有效!
Here is an example, althought I dont recommend it since you are handling integers!!这是一个示例,尽管我不推荐它,因为您正在处理整数!

    switch (charRace) {
        case 1:
            System.out.println("You selected Dwarf");
            break;
        case 2:
            System.out.println("You selected Elf");
            break;
        case 3:
            System.out.println("You selected Human");
            break;
        case 4:
            System.out.println("You selected Orc");
            break;
        default:
            System.out.println("Error: invalid input!");
            break;
    }

If you want this program more sustainable, you should use inherithence.如果你想让这个程序更可持续,你应该使用继承。

Like:喜欢:

public class Orc extends Race{
}

public class Human extends Race{
}

After creation of the all races, you can create a method for creating new race for given number.创建所有种族后,您可以创建一种方法来为给定号码创建新种族。 For example:例如:

public static Race createRace(int raceType){
  if(raceType == 1)
    return new Orc();
  else if(raceType == 2)
    return new Human();
}

Also, you can define different variables and different methods for your races.此外,您可以为您的比赛定义不同的变量和不同的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM