Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6年前关闭。
                    
                
        

class Demo2
{
    int i=1,j=2;
    void fun1()
    {
        i=i+1;
        j=j+1;
    }
    public static void main(String[] args)
    {

        Demo2 d1=new Demo2();
        d1.fun1();
        d1.fun1();
        d1.fun1();
        System.out.println("Hello World!");
    }
}


找不到符号,无法应用func。显示错误。请帮助我,我是一名基本学习者...。

最佳答案

代码中有几个错误。我已经评论并提出了一些有效的代码。

class Demo2
{

    void fun1()
    {
        i=i+1; //i has not been initialized
        j=j+1; //j has not been initialized
    }
    public static void main(String[] args)
    {

        Demo2 d1=new Demo2();
        d1.fun1(1);//"fun1" does not accept a parameter.
        d1.fun1();
        d1.fun1();
        System.out.println("Hello World!");
    }
}


这是一个有效的Demo2类,可以帮助您:

class Demo2
{
    int i = 0;
    int j = 0;

    public void fun1(int param)
    {
        i=i+param;
        j=j+param;
    }

    public static void main(String[] args)
    {
        Demo2 d = new Demo2();
        d.fun1(1);//adds 1 to both i and j
        d.fun1(2);//adds 2 to both i and j
        System.out.println("i is equal to " + i);
        System.out.println("j is equal to " + j);
    }
}

08-05 07:45