How can I add a method or make this code so that I am not repeating myself so many times?

SImplyStupid :

I have developed this code for a Rick and Morty character quiz and the problem I am now facing is repetition I know my code should not be this redundant and maybe the implementation of a method or something might help especially because that same if statement after every question is the same. but I am not sure how to get started so if anyone could help me clean up and or get rid of all the repeating if statements it would be much appreciated. BTW don't mind the repeating question as I plan to fill that in later. I did, however, number it to show that it changes but again the if statement remains the same.

/* Name: Armaan
 * Date: Saturday, November 23rd, 2019
 * Course: Computer Science
 * Description: Rick and Morty quiz
 */

import hsa.Console;
import java.awt.*;

public class RickandMorty
{
  static Console c;
  public static void main(String[] args)
  { 
    c = new Console();

    character rick = new character();
    rick.selections = 0;
//    rick.description = ("");

    character morty = new character();
       morty.selections = 0;
//    morty.description = ("");

    character summer = new character();
       summer.selections = 0;
//    summer.description = ("");

    character jerry = new character();
       jerry.selections = 0;
//    jerry.description = ("");


       while(rick.selections < 5 && morty.selections < 5 && summer.selections < 5 && jerry.selections < 5){

       c.print("1. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice = c.readString();
       if (choice.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice.equalsIgnoreCase("D")){
       jerry.selections++;
       }


      c.print("2. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice1 = c.readString();
       if (choice1.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice1.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice1.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice1.equalsIgnoreCase("D")){
       jerry.selections++;
       }


        c.print("3. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice2 = c.readString();
       if (choice2.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice2.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice2.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice2.equalsIgnoreCase("D")){
       jerry.selections++;
       }


        c.print("4. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice3 = c.readString();
       if (choice3.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice3.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice3.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice3.equalsIgnoreCase("D")){
       jerry.selections++;
       }


        c.print("5. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice4 = c.readString();
       if (choice4.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice4.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice4.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice4.equalsIgnoreCase("D")){
       jerry.selections++;
       }

   if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

         c.print("6. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice5 = c.readString();
       if (choice5.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice5.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice5.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice5.equalsIgnoreCase("D")){
       jerry.selections++;
       }

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

               c.print("7. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice6 = c.readString();
       if (choice6.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice6.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice6.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice6.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

               c.print("8. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice7 = c.readString();
       if (choice7.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice7.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice7.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice7.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

         c.print("9. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice8 = c.readString();
       if (choice8.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice8.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice8.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice8.equalsIgnoreCase("D")){
       jerry.selections++;
       }

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

        c.print("10. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice9 = c.readString();
       if (choice9.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice9.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice9.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice9.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

        c.print("11. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice10 = c.readString();
       if (choice10.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice10.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice10.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice10.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

        c.print("12. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice11 = c.readString();
       if (choice11.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice11.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice11.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice11.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

        c.print("13. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice12 = c.readString();
       if (choice12.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice12.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice12.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice12.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

         c.print("14. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice13 = c.readString();
       if (choice13.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice13.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice13.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice13.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

         c.print("15. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice14 = c.readString();
       if (choice14.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice14.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice14.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice14.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

        c.print("16. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice15 = c.readString();
       if (choice15.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice15.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice15.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice15.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

        c.print("17. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice16 = c.readString();
       if (choice16.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice16.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice16.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice16.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

        c.print("18. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice17 = c.readString();
       if (choice17.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice17.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice17.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice17.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

       c.print("19. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice18 = c.readString();
       if (choice18.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice18.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice18.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice18.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;

       c.print("20. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
       String choice19 = c.readString();
       if (choice19.equalsIgnoreCase("A")){
       rick.selections++;
       }
       if (choice19.equalsIgnoreCase("B")){
       morty.selections++;
       }
       if (choice19.equalsIgnoreCase("C")){
       summer.selections++;
       }
       if (choice19.equalsIgnoreCase("D")){
       jerry.selections++;
       } 

        if (rick.selections == 5) break;
       if (morty.selections == 5) break;
       if (summer.selections == 5) break;
       if (jerry.selections == 5) break;





       }


  }
}
class character {
  int selections;
  String descprition;


}
Ananay Gupta :

If you want to use a method to reduce repitition, you could do this

    import hsa.Console;
    import java.awt.*;

    public class RickandMorty
    {
      static character rick, morty, summer, jerry;
      static Console c;
      public static void main(String[] args)
      { 
        c = new Console();

        rick = new character();
        rick.selections = 0;
    //    rick.description = ("");

        morty = new character();
           morty.selections = 0;
    //    morty.description = ("");

        summer = new character();
           summer.selections = 0;
    //    summer.description = ("");

        jerry = new character();
           jerry.selections = 0;
    //    jerry.description = ("");


           while(rick.selections <= 5 && morty.selections <= 5 && summer.selections <= 5 && jerry.selections <= 5){

           ask("1. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
           ask("2. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |");
        //.. do this for all your questions. If you want, make an array/arraylist with the questions, and run a loop

        //also, I removed the breaks, because they were redundant. They are already the loop condition





           }


      }
    public static void ask(String q){
           c.print(q);
           String choice = c.readString();
           if (choice.equalsIgnoreCase("A")){
           rick.selections++;
           }
           if (choice.equalsIgnoreCase("B")){
           morty.selections++;
           }
           if (choice.equalsIgnoreCase("C")){
           summer.selections++;
           }
           if (choice.equalsIgnoreCase("D")){
           jerry.selections++;
           }
        }

    }
    class character {
      int selections;
      String descprition;


    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Refactoring a library to be async, how can I avoid repeating myself?

Can I simplify this code? And if so how

How to identify the printed results of my program so I can send them to myself

How to I make this not pull up so many tabs at once?

How can I mutate many columns in dplyr without repeating mutate many times?

My code is copy and pasted about 5 times but how can i make it so it's only once?

How do I make this algorithm lazier without repeating myself?

How do I make it so no matter how many times the user presses enter with a blank string, it always prints the 'start' string?

I have a problem array.map() method it loops so many times every time i call that in React

How can I mix the method so that all factors are display? I am missing one more factor

So i have this code that i am using arrays of [60] and how if i add an object can i edit it afterwards in java?

How can I get the root terminal to add myself into sudoers file so that I can act as root?

Can i make site myself?

How can I adjust this code so it only has one array but you make it into an object (so you can work with it as a JObject in the front end)?

how do I loop this code so I can add to the total cost value

How can I modify this code so that I am not replotting my graph everytime, matplotlib

How do I combine my Django models so that I am not repeating myself?

Python repeate my code so many times i want

How can i make a function out of this so i don't have to copy paste the same piece of code?

Why am I getting so many errors?

How can I make this repeating code look neater

somehow I need to update all the functions so that I can get how many times each function is called during the program lifecycle

How can make this java code so that i can only get 3 chances to re enter my password

How can I write this more efficiently without repeating myself?

How can I adapt this selenium code so that it constantly refreshes in a loop for 'n' times?

How can I make a table with 2 column on top and 3 column below. I have tried so many times, tried to look for online tutorials but cannot find any

How do I add a method so I can just a String instead of typing a whole statement

Am I repeating myself too much in my code?

How to add a Appbar to my code, i have tried so many tutorials and i cannot stitch the code code together