当前位置: 首页 > 知识库问答 >
问题:

为什么子方法在主方法中找不到符号?

方航
2023-03-14

新手开始编码,目前正在做一个制作“约会网站”的小项目。我正在完成它,但是我决定编译我的代码,一个错误出现了,尽管我已经创建了一个对象?

仅供参考,我已经创建了一个练习类,看起来像这样:

public class Practice {

    String name;
    short age;
    int height;

    public Practice ( String name, short age, int height ) {
            this.name = name;
            this.age = age;
            this.height = height;
        }
    }
import java.util.Scanner;

public class Dating {

    public static void kathHeightDiff () {
        if ( user1.height > katherine.height )
            System.out.println("You are 5 taller");
        else 
            System.out.println("You are 6 taller");
    }

    public static void main (String[] args) {

        System.out.println("Hello kind sir. Are you interested in love? Press 1 for yes, other numbers for no");
        int userAnswer = IO.readInt();

        if ( userAnswer == 1 ) {
            System.out.println("Nice! What is your name?");
            String userName = IO.readString();
            System.out.println("Cool. What is your age?");
            Scanner age = new Scanner(System.in);
            short userAge = age.nextShort();
            System.out.println("What is your height in inches? 5'0 is 60, 5'6 is 66, 6'0 is 72");
            int userHeight = IO.readInt();

        Practice priya = new Practice( "Priya", (short)19, 62);
        Practice katherine = new Practice ( "Katherine", (short)18, 63);
        Practice jed = new Practice ("Jedidiah", (short)20, 67);
        Practice yourMom = new Practice ("Your mom", (short)53, 61);
        Practice umayma = new Practice ("Umayma", (short)19, 63);
        Practice nawal = new Practice ("Nawal" , (short)20, 64);
        Practice user1 = new Practice( userName, (short)userAge, userHeight);

        System.out.println("Do you have a desired height? If so, press 1");
        int hPreference = IO.readInt();
        System.out.println();

        if ( hPreference == 1 ) {
            System.out.println("How tall in inches? 5'0 is 60, 5'6 is 66, 6'0 is 72 ");
            int desiredHeight = IO.readInt();
            System.out.println();
        }

        System.out.println("Which band member would you rather date?");
        System.out.println();
        System.out.println("A for drummer");
        System.out.println("B for guitarist");
        System.out.println("C for Singer");
        System.out.println("D for piano guy");
        System.out.println();
        char bandMember = IO.readChar();

        switch (bandMember) {
            case 'A':
                System.out.println("You picked A ( Drummer )");
                System.out.println("You sure love your full-arms workout");
                break;
            case 'B':
                System.out.println("You picked B ( Guitarist )");
                System.out.println("All you need is your fingers");
                break;
            case 'C':
                System.out.println("You picked C ( Singer )");
                System.out.println("Maybe you like some mouth action");
                break;
            case 'D':
                System.out.println("You picked D ( Piano Guy )");
                System.out.println("Classic, I see");
                break;
            default:
                System.out.println("No choice? I respect your choice");
                break;

        }

        System.out.println();

        System.out.println("What ball is life?");
        System.out.println();
        System.out.println("A for basketball");
        System.out.println("B for Soccer");
        System.out.println("My balls");
        System.out.println("D for Football");

        System.out.println();

        char ball = IO.readChar();

        switch (ball) {
            case 'A':
                System.out.println("You picked A ( Basketball )");
                System.out.println("You're a baller too");
                break;
            case 'B':
                System.out.println("You picked B ( Soccer )");
                System.out.println("Messi > Ronaldo");
                break;
            case 'C':
                System.out.println("You picked C ( Human balls )");
                System.out.println("Nice");
                break;
            case 'D':
                System.out.println("You picked D ( Football )");
                System.out.println("Ehh");
                break;
            default:
                System.out.println("Just pick something");
                break;

            }

        System.out.println();

        System.out.println("Vacation?");
        System.out.println();
        System.out.println("A for London?");
        System.out.println("B for Seoul?");
        System.out.println("C for Paris?");
        System.out.println("D for Cancun?");
        char trip = IO.readChar();

        System.out.println();

        switch (trip) {
            case 'A':
                System.out.println("You picked A ( London )");
                System.out.println("Brexit or no?");
                break;
            case 'B':
                System.out.println("You picked B ( Seoul )");
                System.out.println("A complimentary visit to North Korea after");
                break;
            case 'C':
                System.out.println("You picked C ( Paris )");
                System.out.println("City of Love, just like my mattress");
                break;
            case 'D':
                System.out.println("You picked D ( Cancun )");
                System.out.println("Everyday is Spring Break");
                break;
            default:
                System.out.println("Get out of the house");

        }

        System.out.println();

        if ( bandMember == 'A' && ball == 'A' && trip == 'A' ) {
            System.out.println("Your match is " + katherine.name + ". Press 1 for details, other to leave");
            int details = IO.readInt();
            if ( details == 1 ) {
                System.out.println("Her name is " + katherine.name );
                System.out.println("Her age is " + katherine.age );
                System.out.println("Her height is " + katherine.height + " inches");
                System.out.println();
                kathHeightDiff();
            }










        } else {
            System.out.println("OK BYE HAVE A WONDERFUL TIME");
        }


    }

}
}

它说它找不到user1。身高和凯瑟琳。在我的kathHeightDiff()方法中,height。我有点困惑,因为当我做Practice user1=new Practice(用户名,(短)年龄,用户身高)时,我不是已经创建了它吗?


共有1个答案

包兴思
2023-03-14

您在main()方法中创建了一个局部变量。因此,其范围是:主要方法。

如果一个变量应该由多个方法使用,则必须将该变量作为参数传递给这些方法调用,或者必须将该局部变量转换为周围类的字段。

 类似资料: