首页 > 解决方案 > 不认识班级

问题描述

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

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

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

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”文件的名称或我如何称呼该类有关。我收到“未声明的变量”错误。

标签: javaclass

解决方案


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


推荐阅读