简体   繁体   English

具有字符串内容的重复方法

[英]Repeating methods with string content

I have a school assignment where I've been asked to write a program that show a figure (a boy, girl or a rocket) in a dialog window depending on the user's choice. 我有一个学校作业,要求我编写一个程序,该程序根据用户的选择在对话框窗口中显示人物(男孩,女孩或火箭)。
While i've managed to do so, but one thing I can't solve. 虽然我设法做到了,但是我无法解决的一件事。 In the assignment, the user is going to have the ability to choose how the rocket's length is going to be by typing in a number. 在分配中,用户将能够通过输入数字来选择火箭的长度。
Based on the number, two of the methods I've created is going to repeat itself to make the rocket's length expand. 根据数字,我创建的两种方法将重复进行以使火箭的长度扩展。 But I haven't been able to come up with anything that does this. 但是我还无法提出任何能做到这一点的东西。

I've been going through various tutorials (if, while, etc.) but my limited experience with Java has left me unsuccessful. 我一直在阅读各种教程(如果有,等等),但是我对Java的有限经验使我无法成功。 Any ideas what to do? 有什么想法怎么办?

    import static javax.swing.JOptionPane.* ;

    public class Figurer 
    {
        public static void main (String [] arg) 
        {
            String hälsning, indata ;
            char svar ;
            hälsning ="Hej!\n"
                 + "Detta program skriver ut en figur\n"
                 + "som du väljer i nästa fönster." ;
            showMessageDialog (null, hälsning) ;

    do
    {
        indata = showInputDialog ("Välj mellan följande figurer:\n"
             + "Pojke\n"
             + "Flicka\n"
             + "Raket") ;

        switch(indata) {
            case "Pojke" :
            case "pojke" :    
                String showBoy = figureBoy () ;
                showMessageDialog (null, showBoy) ;
                break;
            case "Flicka" :
            case "flicka" :    
                String showGirl = figureGirl () ;
                showMessageDialog (null, showGirl) ;
                break;
            case "Raket" :
            case "raket" :
                String showRocket = figureRocket () ;
                showMessageDialog (null, showRocket) ;
                break;

     }  
        indata = showInputDialog ("Vill du se en till figur? (j/n) ") ;   // Repetionssats för fler beräkningar
                svar = indata.charAt(0);
            }
            while (svar == 'j' || svar == 'J') ; 
            }

        //METOD: figureBoy
        //INNEHÅLL: Argument för ihopsättande av figur föreställande en pojke
        //ARGUMENT: boy, part_huvud (), part_base (), part_body () och part_cone () 
        //RETURNERAS: Sammanställningen av argumenten till variabel showBoy
        public static String figureBoy () 
        {
            String boy = part_huvud () ;  
            boy = boy + "\n" + part_base () + "\n" + part_body () + "\n"
            + part_base () + "\n" + part_cone () ;
            return boy;
        }
        //METOD: figureGirl
        //INNEHÅLL: Argument för ihopsättande av figur föreställande en flicka
        //ARGUMENT: girl, part_cone () och part_base () 
        //RETURNERAS: Sammanställningen av argumenten till variabel showGirl
        public static String figureGirl () 
        {
            String girl = part_huvud () ;  
            girl = girl + "\n" + part_cone () + "\n" + part_base () + "\n"
            + part_cone () ;
            return girl;
        }
        //METOD: figureRocket
        //INNEHÅLL: Argument för ihopsättande av figur föreställande en raket
        //ARGUMENT: rocket, part_base (), part_body () och part_cone 
        //RETURNERAS: Sammanställningen av argumenten till variabel showRocket
        public static String figureRocket () 
        {

        }
        //METOD: part_huvud
        //INNEHÅLL: Argument med tecken som föreställer ett huvud
        //ARGUMENT: Variabeln hu och sträng med tecken som formar huvudet 
        //RETURNERAS: Innehållet i variabeln hu som skickas till metoden figureBoy, figureGirl och figureRocket
        public static String part_huvud ()
        {
            String hu ;
            hu = "  /      \\ \n"
            + " |@,@| \n"
            + "  \\  v  /" ;
            return hu;
        //METOD: part_base
        //INNEHÅLL: Argument med tecken som föreställer ett tjockare streck
        //ARGUMENT: Variabeln ba och sträng med tecken som formar strecket 
        //RETURNERAS: Innehållet i variabeln ba som skickas till metoden figureBoy, figureGirl och figureRocket
        }
        public static String part_base ()
        {
            String ba ;
            ba = "=====" ;
            return ba  ;
        //METOD: part_body
        //INNEHÅLL: Argument med tecken som föreställer två paralella linjer
        //ARGUMENT: Variabeln ba och sträng med tecken som formar linjerna 
        //RETURNERAS: Innehållet i variabeln bo som skickas till metoden figureBoy, figureGirl och figureRocket    
        }
        public static String part_body ()
        {
            String bo ;
            bo = "|         |\n"
            +"|         |\n"   
            +"|         |" ;    
            return bo ;
        //METOD: part_cone
        //INNEHÅLL: Argument med tecken som föreställer en kona
        //ARGUMENT: Variabeln ba och sträng med tecken som formar konan 
        //RETURNERAS: Innehållet i variabeln co som skickas till metoden figureBoy, figureGirl och figureRocket    
        }
        public static String part_cone ()
        {
            String co ;
            co = "     /\\\n"
            +"   /    \\      \n"   
            +" /        \\       " ;    
            return co ;
        }
}   

Not going to answer and write all the code, but here is some direction. 不会回答并编写所有代码,但这是一些方向。

First, when it comes to Strings you can change to lowercase to do case-insensitive comparison: 首先,在字符串方面,您可以更改为小写形式以进行不区分大小写的比较:

indata = indata.toLowerCase();

That way you don't need to double all your cases and you'll match even if they uppercase the second letter by accident. 这样一来,您无需将所有情况都加倍,即使它们意外地将第二个字母大写,您也可以匹配。 And if you ever need to do equals instead of a switch do: 而且,如果您需要做均等而不是切换,请执行以下操作:

indata.equalsIgnoreCase("some value");

Or even better, to avoid null: 甚至更好,以避免null:

"someotherstring".equalsIgnoreCase(indata);

Second, you can build a string of any length during a loop just as you would add numbers in a loop. 其次,您可以在循环中构建任意长度的字符串,就像在循环中添加数字一样。 But to accomplish this use a StringBuilder object: 但是要实现此目的,请使用StringBuilder对象:

StringBuilder rocketBody = new StringBuilder();
int howmanypieces = (user input);
while (howmanypieces > 0) {

   rocketBody.append(rocketBodyPart());
   howmanypieces--;

}
rocketBody.toString();

If you have an integer variable count , and you want to run some code count times, do this: 如果您有一个整数变量count ,并且想要运行一些代码count时间,请执行以下操作:

for (int i = 0; i < count; i++) {
    // code
}

What this does is first make a variable called i and initializes it to 0 , then repeats the code until i >= count . 首先要做的是创建一个名为i的变量并将其初始化为0 ,然后重复执行该代码,直到i >= count为止。 It also adds 1 to i after running the code. 运行代码后,它还会向i 1

Inside the loop, you can use the variable i . 在循环内部,可以使用变量i The first time the loop runs, it will be 0 . 循环第一次运行时,它将为0 The next time it will be 1 , and so on. 下次将是1 ,依此类推。 It will never be equal to count . 它永远不会等于count In the last iteration, it will be count - 1 . 在上一次迭代中,它将为count - 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM