繁体   English   中英

无法识别 class

[英]Not recognizing the class

这是用于调用名为 Couple 的 class 的代码,但它无法识别 class 为什么会这样?

public class AgencyInterFace {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        Couple c = new Couple();
        int choice, position;

        showSelection();
        choice = console.nextInt();
        while (choice != 9) {
            switch (choice) {
                case 1:
                    addCouple();
                    break;
                case 2:
                    position = console.nextInt();
                    testCouple(position);
                    break;
                case 3:
                    position = console.nextInt();
                    displayCouple(position);
                    break;
                case 9:
                    break;
                default:
                    System.out.println("Invalid Selection");
            } //end switch
            showSelection();
            choice = console.nextInt();
        }
    }

    public static void showSelection() {
        System.out.println("Select and enter");
        System.out.println("1 - add a new couple");
        System.out.println("2 - test a couple");
        System.out.println("3 - display couple");
        System.out.println("9 - exit");
    }

    public static void addCouple() {
        Scanner console = new Scanner(System.in);
        String herName, hisName;
        int herAge, hisAge, ageDiff;

        System.out.print("her name: ");
        herName = console.nextLine();
        System.out.print("her age: ");
        herAge = console.nextInt();
        System.out.print("his name: ");
        hisName = console.nextLine();
        System.out.print("his age: ");
        hisAge = console.nextInt();

        ageDiff = herAge - hisAge;
        c.addData(herName, herAge, ageDiff, hisName, hisAge, ageDiff);
    }

    public static void testCouple(int position) {
        System.out.println(c.test(position));
    }

    public static void displayCouple(int position) {
        System.out.println(c.display(position));
    }

    public static void averageAge(int position) {
        System.out.println(c.avgAge());
    }

    public static void maxDifference(int position) {
        System.out.println(c.maxDif(position));
    }

    public static void averageDifference(int position) {
        System.out.println(c.avgDif(position));
    }
}//end of class

此代码是 class,它是要被调用但未被识别且无法调用的代码。

public class Couple {
    final private int MAX = 5;
    private Person[] p1, p2;
    private int total;

    public Couple() {
        p1 = new Person[MAX];
        p2 = new Person[MAX];
        total = 0;
    }

    private void setData1(Person p, String name, int age, int ageDiff) {
        p.setName(name);
        p.setAge(age);
    }

    public String test(int pos) {
        if (pos != -1) {
            if (p1[pos].getAge() < p2[pos].getAge()) return ("GOOD FOR 
            "+p2[pos].getName()+" !");
            else return ("GOOD 
            FOR "+p1[pos].getName()+" !");
        }
        return "error";
    }

    public void addData(String name1, int age1, int ageDiff1, String
            name2, int age2, int ageDiff2) {
        p1[total] = new Person();
        p2[total] = new Person();
        setData1(p1[total], name1, age1, ageDiff1);
        setData1(p2[total], name2, age2, ageDiff2);
        total++;
    }

    public String display(int position) {
        if (position != -1)
            return ("p1: " + p1[position].getName() + " 
        "+p1[position].getAge()+" / n "+" p2:
        "+p2[position].getName()+"
        "+p2[position].getAge());
                else
        return ("error");
    }

    public String avgAge(int position) {
        double avg = 0;
        double sum = 0.0;
        for (int i = 0; i < position; i++) {
            sum += p1[total].getAge();
            sum += p2[total].getAge();
        }
        avg = sum / position;
        return ("The average age is: " + avg);
    }

    public void ageDifference(int position) {
        double ageDif = 0.0;
        double ageSum = 0.0;
        for (int i = 0; i < position; i++) {
            if (p1[total].getAge() < p2[total].getAge()) {
                ageSum = p2[total].getAge() - p1[total].getAge();
            } else {
                ageSum = p1[total].getAge() - p2[total].getAge();
            }
            ageSum = ageDif;
        }
    }
}

这是否与“Couple”文件的名称或我如何称呼 class 有关。 我收到“未声明的变量”错误。

您在main()方法中定义c 因此,它在您的其他方法中不可见。 要么将 c 作为参数传递给其他方法,要么将其AgencyInterFace class 的(静态)属性,而不是main()的局部变量。

使用 STATIC 方法

如果要调用 class 的方法,例如test(int position) ,而不创建变量 c,则需要使此方法为 static:

public static String test(int pos) {
   if (pos!=-1)  {
        if (p1[pos].getAge()<p2[pos].getAge()) return("GOOD FOR "+p2[pos].getName()+"!");
        else return("GOOD FOR"+p1[pos].getName()+"!");
       }
       return "error";
}

在这种情况下,您的 arrays p1[]p2[]也需要为 static --> 它们只会被创建一次。

以及制作 arrays static 的示例:

private static Person[] p1 = new Person[MAX],
    p2 = new Person[MAX];

现在您可以使用Couple.test(position)调用 class 的此方法,它将返回一个字符串。

使用非静态方法

如果要创建 class Couple 的多个引用,因为 p1[] 和 p2[] 应该包含不同的值,您需要创建 class Couple 的引用。

您可以通过告诉程序 c 是什么来实现这一点:

Couple c = new Couple();

编辑:

我看到您创建了一对,但不是在正确的位置。 如果你在main()方法中创建你的情侣,你不能在除了这个方法之外的任何地方使用它。 您应该在 class 中声明它:

public class AgencyInterFace {
    private static Couple c = new Couple(); //<-- here
    
    // main-method

    // other methods
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM