Simple Java Method won't run. Method inside method

↘锁芯ラ 提交于 2020-01-11 11:35:31

问题


Here is my code:

public class Project1mrybak3
{   
    public static void main (String[] args)   
    {
        System.out.println("Begin Java Exection");  
        System.out.println("");

        // put your Java Program here

        //choose picture

        String  picfile = FileChooser.pickAFile();  
        Picture pic     = new Picture(picfile);

        //Create turtle 1   
        Turtle t1 = new Turtle (pic);   
        t1.setPenWidth(10);

        //Create turtle 2  
        Turtle t2 = new Turtle (pic);

        flower(t1,200);

        //show picture    
        pic.show();

        System.out.println("");     
        System.out.println("End Java Exection");
    }//end of main class


    public static void flower (Turtle tparam, int x )     
    {     
        tparam.forward(x);     
    }

    public static void flowers ()       
    {      
        flower(t1,15);    
    }
} // end of Template class

So as you can tell, it doesn't make much sense but I have just started writing it. My question is, for the last method flowers, when I try to run it, it says that it cannot find the symbol variable t1. If I take out the parameters, it says that it requires parameters Turtle and int. Can I not put methods inside of methods?

Ultimately my goal is to create about 4 methods to draw parts of the flower, then put them all inside of one method, then inside my main code, I can call that method with turtle t1 and some x variable.

Thank you for any help and your time!


回答1:


No, you can't put methods inside of methods.

If you need to use multiple methods and it doesn't make sense to pass your variables as parameters, you should make the variables you need into fields (or members) of a class (read that link it will help). Move their declaration outside of the method they are local to (such as main) and into the scope of the class itself. Then, all methods in that class can refer to those variables:

  • With this. prefixed
  • Or just as they are, as long as there is no local variable with the same name.



回答2:


Like NickC said, you can't put methods inside of methods. Something else important to note here, though, is that t1 in your example isn't a method; it's an object (Method objects from the reflection API being a little beyond the scope of this question...). I don't mean to presume your design, but it seems like you might want your various flower-drawing methods inside a separate Turtle class, and you can have another method inside that class that calls all the helper methods in turn, like so:

public void drawFlower(int xPosition) {
    drawStem(xPosition);
    drawStamen(xPosition);
    drawPetals(xPosition);
    ...
}

That way, all of those helper methods have access to the Turtle that's (I think) doing the drawing (via the this keyword, as NickC pointed out), and they can return a modified xPosition if you want so that you can pass a different starting point into the next method.




回答3:


You cannot define methods within methods, but you can call methods inside methods - such as to re-run a method in a loop with possibly differing parameters until a condition is met.



来源:https://stackoverflow.com/questions/19105892/simple-java-method-wont-run-method-inside-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!