gpt4 book ai didi

java - 如何向用户询问一组问题并存储答案

转载 作者:行者123 更新时间:2023-11-30 02:35:14 25 4
gpt4 key购买 nike

我正在制作一款基于文本的冒险游戏。我试图让游戏根据玩家对某些问题的回答来选择“类别”。 (例如,如果玩家选择偷偷摸摸或 secret 倾向的答案,那么游戏将为他们分配适合此类人的“流氓”类别。

到目前为止,我已经创建了这个方法,我想稍后在主程序中调用它,然后根据返回值,我将使用另一个条件语句通过调用 set 方法来设置玩家类.

这是到目前为止我的方法。按照我的方向,我将有一个非常非常长的 selectSpec 方法,因为我至少想要 10 个问题。我的具体问题是,最有效的方法是什么?用一种方法完成这一切是正确的方法吗?

public static int chooseSpec(Scanner in) {
int war = 0; //counter for warrior class
int rog = 0; //counter for rogue class
int mag = 0; //counter for magic class
int choice; //users choice, a number between 1 and 3.

System.out.println("While out hunting, you come across a deer which has been badly mauled by a wolf. What do you do?" + "\n" +
"1. Draw your dagger and end it's suffering" + "\n" +
"2. Attempt to heal it with a herbal concoction, knowing it may not work." + "\n" +
"3. Leave it, and allow nature to take it's course.");
choice = in.nextInt();

switch (choice) {
case 1:
war += 1;
break;

case 2:
mag += 1;
break;
case 3:
rog +=1;
}
return choice;
}

最佳答案

您应该将所有问题和答案存储在一个对象内。这样,您可以混合每个问题的答案,目前选择始终 1 将保证相同的答案。

其次,它允许您为每个问题添加任意数量的答案,甚至可以在每次开始游戏时混合它们的顺序。

在这里使用更多的 java 抽象也很好:

public ClassType chooseSpec(List<Question> questions, Scanner in) {

List<ClassType> selectedAnswers = new ArrayList<>(questions.size());
// iterate over all your questions
for(Question question: questions) {
// print question and all answers
System.out.println(question.getQuestion() + "\n"
+ question.getAnswers().stream().map(Answer::getQuestion)
.collect(Collectors.joining("\n")));

int choice = in.nextInt();
if(choice >= question.getAnswers().size()){
// check if answer is within the scope
throw IllegalArgumentException("There are only "+question.getAnswers() +" answers");
}
// add selected answer type to global result list
selectedAnswers.add(question.getAnswers().get(choice-1).getClassType());
}

return findMostPolularClass(selectedAnswers);
}

private ClassType findMostPolularClass(List<ClassType> selectedAnswers){
// might be too fancy, but it will find most frequent answer
Map<ClassType, Long> occurences = selectedAnswers.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting()));
return occurences.entrySet().stream()
.max(Comparator.comparing(Map.Entry::getValue)).get().getKey();
}

保存问题和所有答案:

static class Question {

private final String question;
private final List<Answer> answers;

public Question(String question, List<Answer> answers) {
this.question = question;
this.answers = answers;
}

public String getQuestion() {
return question;
}

public List<Answer> getAnswers() {
return answers;
}
}

每个答案都定义了答案文本,以及它最适合哪个类

class Answer {

private final String question;
private final ClassType classType;
// static constructors, help to work with this model
public static Answer mageAnswer(String question) {
return new Answer(question, ClassType.MAGE);
}

public static Answer wariorAnswer(String question) {
return new Answer(question, ClassType.WARIOR);
}

private Answer(String question, ClassType classType) {
this.question = question;
this.classType = classType;
}

public ClassType getClassType() {
return classType;
}

public String getQuestion() {
return question;
}
}

//将所有类存储在这里,它比记住数字更具可读性:)

enum ClassType {
MAGE, WARIOR, ARCHER
}

关于java - 如何向用户询问一组问题并存储答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43277580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com