-4

そこで私はこのリック・アンド・モーティ・クイズのコードを作成し、コンピューター・サイエンスの先生が配列を追加することを提案しました。それが私のコードの機能にどのように役立つか、またはどのように行うべきかわかりませんでしたが、他の誰かが同じ提案を持っている場合は、実装方法を教えてください. 前もって感謝します!

/* 名前: 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;


    }
4

1 に答える 1

3

配列は、まとめておきたい類似または関連するものがたくさんある場合に使用されます。

あなたの場合、同じように処理する一連の質問があります(質問文字列ごとに、それを呼び出します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);
}
于 2019-11-26T02:22:19.933 に答える