简单的Java方法将无法运行.方法内部 [英] Simple Java Method won't run. Method inside method

查看:80
本文介绍了简单的Java方法将无法运行.方法内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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

如您所知,这没有多大意义,但是我才刚刚开始编写它.我的问题是,对于最后一个方法flowers,当我尝试运行它时,它说它找不到符号变量t1.如果我删除参数,它说它需要参数Turtleint.我不能将方法放入方法内部吗?

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?

最终我的目标是创建大约4种方法来绘制花朵的一部分,然后将它们全部放入一个方法中,然后再放入主代码中,我可以使用turtle t1和一些变量.

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!

推荐答案

不,您不能将方法放在方法内部.

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:

  • this.前缀
  • 或者照原样,只要不存在具有相同名称的局部变量即可.

这篇关于简单的Java方法将无法运行.方法内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆