【问题标题】:Why can't sub-method find symbol in the main method? [duplicate]为什么子方法在主方法中找不到符号? [复制]
【发布时间】:2019-04-07 03:35:08
【问题描述】:

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

仅供参考,我已经创建了一个如下所示的 Practice 类:

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");
        }


    }

}
}

它说在我的kathHeightDiff() 方法中找不到user1.heightkatherine.height。我有点困惑,因为我在创建 Practice user1 = new Practice(userName, (short)age, userHeight) 时不是已经创建了吗?

【问题讨论】:

  • 无关:你应该每 5 分钟运行一次编译器(任何时候你添加了一些有意义的东西并且你认为“这应该现在编译”)。尽可能频繁地运行编译器,并立即修复所有错误/警告。
  • @GhostCat 或者,使用 IDE 并继续编译。
  • 骗子解释了变量范围是什么,同样适用于方法。您不能在方法中声明变量(在这种情况下为main),然后期望它可以在其他任何地方访问。
  • Java 中没有“子方法”之类的东西。这可能有助于您理解为什么它不起作用(结合重复)。

标签: java object methods


【解决方案1】:

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

如果一个变量应该被多个方法使用,您必须将该变量作为参数传递给这些方法调用,或者您必须将该 local 变量转换为周边班级。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2014-01-18
    • 2013-10-05
    相关资源
    最近更新 更多