idea报错:java找不到符号

package com.strlen;

class SanWei{
    int b;//胸围
    int w;//腰围
    int h;//臀围
}

public class People5 {
    void speak(int age) {
        System.out.println("I'm " +age+ " years old, and my body is: "+sanWei.b+" "+sanWei.h+" "+sanWei.w);
        age=24;
    }


    public static void main(String[] args) {
        People5 xiaoli=new People5();
        int age=23;
        SanWei sanWei=new SanWei();//initialize
        sanWei.b=90;
        sanWei.w=60;
        sanWei.h=90;
        xiaoli.speak(age);
        System.out.println(age);
    }


}

img
显示找不到符号

要把sanWei对象当做参数传到speak方法里面,修改后代码如下:


class SanWei{
    int b;//胸围
    int w;//腰围
    int h;//臀围
}

public class People5 {
    void speak(int age,SanWei sanWei) {
        System.out.println("I'm " +age+ " years old, and my body is: "+sanWei.b+" "+sanWei.h+" "+sanWei.w);
        age=24;
    }


    public static void main(String[] args) {
        People5 xiaoli=new People5();
        int age=23;
        SanWei sanWei=new SanWei();//initialize
        sanWei.b=90;
        sanWei.w=60;
        sanWei.h=90;
        xiaoli.speak(age,sanWei);
        System.out.println(age);
    }


}

img
希望对你有帮助

class SanWei{
int b;//胸围
int w;//腰围
int h;//臀围
}
这个类定义在另一个java文件中,不要放一起。

换成如下写法 :

package com.strlen;

class SanWei {
    int b;//胸围
    int w;//腰围
    int h;//臀围
}

public class People5  {
    void speak(int age, SanWei sanWei) {
        System.out.println("I'm " + age + " years old, and my body is: " + sanWei.b + " " + sanWei.h + " " + sanWei.w);
        age = 24;
    }

    public static void main(String[] args) {
        People5 xiaoli = new People5();
        int age = 23;
        SanWei sanWei = new SanWei();//initialize
        sanWei.b = 90;
        sanWei.w = 60;
        sanWei.h = 90;
        xiaoli.speak(age, sanWei);
        System.out.println(age);
    }

}


这个问题说透了,是对变量的作用域不清楚