java - How to ask a user a set of questions and store the answers -


i'm making text based adventure game. i'm trying have game choose "class" player based on answers questions. (e.g. if player chooses sneaky or stealthy leaning answers game assign them "rogue" class fits type of person.

so far, i've created method want call later in main program, , based on return value is, i'll use conditional statement set player class calling set method.

here method far. way i'm heading, have very long choosespec method want @ least maybe 10 questions. question efficient way write out, , doing in 1 method right way go?

public static int choosespec(scanner in) {     int war = 0; //counter warrior class     int rog = 0; //counter rogue class     int mag = 0; //counter magic class     int choice; //users choice, number between 1 , 3.      system.out.println("while out hunting, come across deer has been badly mauled wolf. do?" + "\n" +                        "1. draw dagger , end it's suffering" + "\n" +                        "2. attempt heal herbal concoction, knowing may not work." + "\n" +                        "3. leave it, , allow nature 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; } 

you should store questions , answers inside object. way, can mix answers each question, choosing 1 guarantee same answer.

secondly allow add number of answers per question , mix order each time starting game.

it's use more java abstraction here:

public classtype choosespec(list<question> questions, scanner in) {       list<classtype> selectedanswers = new arraylist<>(questions.size());       // iterate on questions     for(question question: questions) {         // print question , 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 within scope           throw illegalargumentexception("there "+question.getanswers() +" answers");         }         // add selected answer type global result list          selectedanswers.add(question.getanswers().get(choice-1).getclasstype());     }      return findmostpolularclass(selectedanswers); }  private classtype findmostpolularclass(list<classtype> selectedanswers){     // might fancy, find 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();  } 

holds question , answers:

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;     } } 

each answer defined answer text, , class belongs best

class answer {      private final string question;     private final classtype classtype;     // static constructors, work 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;     } } 

// store classes here, it's more readable, remembering numbers:)

enum classtype {     mage, warior, archer } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -