Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我怎样才能添加一个方法或生成这个代码,这样我就不会重复这么多次了?_Java - Fatal编程技术网

Java 我怎样才能添加一个方法或生成这个代码,这样我就不会重复这么多次了?

Java 我怎样才能添加一个方法或生成这个代码,这样我就不会重复这么多次了?,java,Java,我已经为Rick和Morty的角色测验开发了这段代码,我现在面临的问题是重复。我知道我的代码不应该如此冗余,也许实现一个方法或其他东西可能会有所帮助,特别是因为每个问题后的相同if语句都是相同的。但我不知道如何开始,所以如果有人能帮我清理和/或摆脱所有重复的if声明,我将不胜感激。顺便说一句,不要介意重复这个问题,因为我计划稍后再补充。然而,我对它进行了编号,以表明它发生了变化,但if语句仍然保持不变 /* Name: Armaan * Date: Saturday, November 23r

我已经为Rick和Morty的角色测验开发了这段代码,我现在面临的问题是重复。我知道我的代码不应该如此冗余,也许实现一个方法或其他东西可能会有所帮助,特别是因为每个问题后的相同if语句都是相同的。但我不知道如何开始,所以如果有人能帮我清理和/或摆脱所有重复的if声明,我将不胜感激。顺便说一句,不要介意重复这个问题,因为我计划稍后再补充。然而,我对它进行了编号,以表明它发生了变化,但if语句仍然保持不变

/* 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;


}
/*名称:Arman
*日期:2019年11月23日星期六
*课程:计算机科学
*描述:里克和莫蒂测验
*/
导入hsa.Console;
导入java.awt.*;
公共级里肯德莫蒂
{
静态控制台c;
公共静态void main(字符串[]args)
{ 
c=新控制台();
字符瑞克=新字符();
rick.selections=0;
//rick.description=(“”);
character morty=新字符();
morty.selections=0;
//morty.description=(“”);
字符summer=新字符();
summer.selections=0;
//summer.description=(“”);
字符jerry=新字符();
jerry.selections=0;
//jerry.description=(“”);
while(rick.selections<5&&morty.selections<5&&summer.selections<5&&jerry.selections<5){
c、 印刷体(“1.选择恐惧、责任、蜘蛛、死亡、孤独);
字符串选择=c.readString();
if(选择同等信号案例(“A”)){
rick.selections++;
}
if(选择同等信号情况(“B”)){
morty.selections++;
}
if(选择同等信号情况(“C”)){
summer.selections++;
}
if(选择同等信号情况(“D”)){
jerry.selections++;
}
c、 印刷体(“2.选择恐惧、责任、蜘蛛、死亡、孤独);
String choice1=c.readString();
如果(选择1.同等信号情况(“A”)){
rick.selections++;
}
if(选择1.同等信号情况(“B”)){
morty.selections++;
}
如果(选择1.同等信号情况(“C”)){
summer.selections++;
}
如果(选择1.同等信号情况(“D”)){
jerry.selections++;
}
c、 印刷体(“3.选择恐惧、责任、蜘蛛、死亡、孤独);
String choice2=c.readString();
如果(选择2.同等信号情况(“A”)){
rick.selections++;
}
如果(选择2.同等信号情况(“B”)){
morty.selections++;
}
如果(选择2.同等信号情况(“C”)){
summer.selections++;
}
如果(选择2.同等信号情况(“D”)){
jerry.selections++;
}
c、 印刷体(“4.选择恐惧、责任、蜘蛛、死亡、孤独);
String choice3=c.readString();
如果(选择3.同等信号情况(“A”)){
rick.selections++;
}
如果(选择3.同等信号情况(“B”)){
morty.selections++;
}
如果(选择3.同等信号情况(“C”)){
summer.selections++;
}
如果(选择3.同等信号情况(“D”)){
jerry.selections++;
}
c、 印刷体(“5.选择恐惧、责任、蜘蛛、死亡、孤独);
String choice4=c.readString();
如果(选择4.同等信号情况(“A”)){
rick.selections++;
}
如果(选择4.同等信号情况(“B”)){
morty.selections++;
}
如果(选择4.同等信号情况(“C”)){
summer.selections++;
}
if(选择4.同等信号情况(“D”)){
jerry.selections++;
}
如果(rick.selections==5)中断;
如果(morty.selections==5)中断;
如果(summer.selections==5)中断;
如果(jerry.selections==5)中断;
c、 印刷体(“6.选择恐惧、责任、蜘蛛、死亡、孤独);
String choice5=c.readString();
如果(选择5.同等信号情况(“A”)){
rick.selections++;
}
if(选择5.同等信号情况(“B”)){
morty.selections++;
}
如果(选择5.同等信号情况(“C”)){
summer.selections++;
}
if(选择5.同等信号情况(“D”)){
jerry.selections++;
}
如果(rick.selections==5)中断;
如果(morty.selections==5)中断;
如果(summer.selections==5)中断;
如果(jerry.selections==5)中断;
c、 印刷体(“7.选择恐惧、责任、蜘蛛、死亡、孤独);
String choice6=c.readString();
如果(选择6.同等信号情况(“A”)){
rick.selections++;
}
如果(选择6.同等信号情况(“B”)){
morty.selections++;
}
如果(选择6.同等信号情况(“C”)){
summer.selections++;
}
如果(选择6.同等信号情况(“D”)){
jerry.selections++;
} 
如果(rick.selections==5)中断;
如果(morty.selections==5)中断;
如果(summer.selections==5)中断;
如果(jerry.selections==5)中断;
c、 印刷体(“8.选择恐惧、责任、蜘蛛、死亡、孤独);
String choice7=c.readString();
如果(选择7.同等信号情况(“A”)){
rick.selections++;
}
如果(选择7.同等信号情况(“B”)){
morty.selections++;
}
如果(选择7.同等信号情况(“C”)){
summer.selections++;
}
如果(选择7.同等信号情况(“D”)){
jerry.selections++;
} 
如果(rick.selections==5)中断;
如果(morty.selections==5)中断;
如果(summer.selections==5)中断;
如果(jerry.selections==5)中断;
c、 印刷体(“9.选择恐惧、责任、蜘蛛、死亡、孤独);
String choice8=c.readString();
如果(选择8.同等信号情况(“A”)){
rick.selections++;
}
如果(选择8.equa
public class RickandMorty {
    private static final String[] QUESTIONS = {
        "1. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "2. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "3. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "4. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "5. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "6. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "7. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "8. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "9. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "10. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "11. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "12. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "13. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "14. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "15. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "16. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "17. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "18. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "19. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
        "20. Pick a fear | A Responsibility | B Spiders | C Death | D Being Alone |",
    };

    private character rick = new character();
    private character morty = new character();
    private character summer = new character();
    private character jerry = new character();

    public static void main(String[] args) {
        new RickandMorty().run();
    }
    private void run() {
        Console c = new Console();
        OUTER: for (;;) {
            for (String question : QUESTIONS) {
                c.print(question);
                String choice = c.readString();
                if (choice.equalsIgnoreCase("A")) {
                    rick.selections++;
                    if (rick.selections == 5) break OUTER;
                }
                if (choice.equalsIgnoreCase("B")) {
                    morty.selections++;
                    if (morty.selections == 5) break OUTER;
                }
                if (choice.equalsIgnoreCase("C")) {
                    summer.selections++;
                    if (summer.selections == 5) break OUTER;
                }
                if (choice.equalsIgnoreCase("D")) {
                    jerry.selections++;
                    if (jerry.selections == 5) break OUTER;
                }
            }
        }
    }
}
    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;


    }