首页 > 解决方案 > 我如何以及应该如何在我的代码中实现一个数组?

问题描述

所以我创建了这个代码,它是一个 rick and Morty 测验,我的计算机科学老师建议添加一个数组。我没有看到它如何帮助我的代码的功能或我应该如何做,但如果其他人有相同的建议,请告诉我如何实现一个。提前致谢!

/* 名称:Armaan * 日期:2019 年 11 月 23 日,星期六 * 课程:计算机科学 * 描述:瑞克和莫蒂测验 */

 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. What would you most likely be doing a t a party? | [A] Insulting everyone without meaning | [B] Trying to make new friends  | [C] Getting as wasted as possible | [D] Following around someone you love |");
           ask(" 3. Pick a super power! | [A] I already have all the super powers I need | [B] Invincibility | [C] Invisibility| [D] Flight |");
           ask(" 4. How many fries could you eat in one sitting? | [A] Keep them coming forever | [B] 100 | [C] Fries? Ewwww. | [D] 50 |");
           ask(" 5. Pick a language not english! | [A] I'd make my own | [B] Japanese | [C] Chinese | [D] Spamish |");
           ask(" 6. Do you care about cleaning? | [A] Who cares? Nothing matters. | [B] Obviously | [C] Not sure. What does everyone else think? | [D] Not bothered |");
           ask(" 7. Which of these is your favourite vegetable? | [A] Pickle | [B] I don't like veggies | [C] I love all veggies | [D] Peas |");
           ask(" 8. Whats your favourite music? | [A] EDM | [B] Rap | [C] Pop | [D] Acoustic |");
           ask(" 9. How would you stop a fight between two friends? | [A] Kill em both | [B] Try and talk it out | [C] Point out their both pathetic | [D] Run away and hide |");
           ask("10. Pick a something you can't live without! | [A] I don't need anything | [B] Laptop | [C] Phone | [D] Family |");
           ask("11. Ideal Birthday? | [A] Huge party held in my honor | [B] Candlelit dinner with my dream girl | [C] A huge part with cool people | [D] Being Alone |");
           ask("12. Choose a color! | [A] Black | [B] Red | [C] Pink | [D] Yellow |");
           ask("13. What's your dream? | [A] Dreams are stupid only reality exists | [B] To live a happy life | [C] Don't know yet | [D] Find someone to take care of me |");
           ask("14. Pick a drink! | [A] Anything alchohol | [B] Soda | [C] Coffee | [D] Juice |");
           ask("15. Pick the weather!| [A] Who cares | [B] Sun | [C] Cloudy | [D] Clear Skies |");
           ask("16. Pick a Vehicle! | [A] Spaceship | [B] Walk| [C] Car | [D] Bike |");
           ask("17. Favourite Food?| [A] I'll stick with my alchohol | [B] Pizza | [C] Hotdog | [D] Ice Cream |");
           ask("18. Pick a pet| [A] Anything dangerous | [B] Dog | [C] Pets are gross | [D] Hamster |");
           ask("19. Pick your footwear! | [A] Who cares | [B] Sneakers | [C] Heels | [D] Bare feet |");
           ask("20. Who's the best? | [A] Armaan | [B] Nikunj | [C] Kenny | [D] John |");


           }

           if (rick.selections == 5){
           c.print(rick.description);
           }

           if (morty.selections == 5){
           c.print(rick.description);
           }

           if (summer.selections == 5){
           c.print(rick.description);
           }


           if (jerry.selections == 5){
           c.print(rick.description);
           }


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


    }

标签: java

解决方案


当您有一堆相似或相关的东西要放在一起时,就会使用数组。

在您的情况下,您有一堆相同处理的问题(对于每个问题字符串,您都会调用ask("...")它。

因此,您可以将问题移动到数组中:

String[] questions = {
    " 1. Pick a fear! | [A] Responsibility | [B] Spiders | [C] Death | [D] Being Alone |",
    " 2. What would you most likely be doing a t a party? | [A] Insulting everyone without meaning | [B] Trying to make new friends  | [C] Getting as wasted as possible | [D] Following around someone you love |",
    " 3. Pick a super power! | [A] I already have all the super powers I need | [B] Invincibility | [C] Invisibility| [D] Flight |"
    // and so on
}

然后遍历你的数组,询问他们:

for(String question : questions) {
    ask(question);
}

推荐阅读