简体   繁体   English

来自主 class 的输入无法在另一个 class 中访问,只会显示初始化值

[英]The input from main class can't be accessed in the another class and would only display the initialized value

I need to access the inputs from the main class and let them be applied to the department class, however, it won't let me.我需要访问主要 class 的输入,并将它们应用到部门 class,但是,它不允许我。 Also, I am not allowed to use parameters.另外,我不允许使用参数。 I need to follow this UML diagram.我需要遵循这个 UML 图。 Please guide me on how can I fix this.请指导我如何解决这个问题。 Our topic is all about inheritance, static variables and method, method overriding and super keyword.我们的主题是关于 inheritance、static 变量和方法、方法覆盖和 super 关键字。

在此处输入图像描述

public class Department {
    private int groupScore;
    private double groupAverage;
    private static int overallScore;
    private static double overallAverage;
    
    public Department(){
        int a = 0; 
        int b = 0; 
        int c = 0; 
        int d = 0; 
        int e = 0;
        
        int gscore = groupScore;
        gscore = a + b + c + d + e;
        
        int oascore = overallScore;
        oascore = overallScore + groupScore;
        
        overallAverage = overallAverage/10.0;
        
        groupAverage = groupScore/5.0;
    }
    public double getGroupAverage() {
        return groupAverage;
    }
    public static void displayAverage() {
        double oave = overallAverage;
        System.out.println("Overall Average: " + oave);
    }
}

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Department objdept = new Department();
        
        System.out.println("Enter 5 rates for Department A (1-5)");
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int d = sc.nextInt();
        int e = sc.nextInt();
        System.out.println("Department 1 group average: " + objdept.getGroupAverage());
        objdept.getGroupAverage();
        objdept.displayAverage();
        System.out.println();
        System.out.println("Enter 5 rates for Department B (1-5)");
        int f = sc.nextInt();
        int g = sc.nextInt();
        int h = sc.nextInt();
        int i = sc.nextInt();
        int j = sc.nextInt();
        objdept.getGroupAverage();
        System.out.println("Department 2 group average: "+ objdept.getGroupAverage());
        System.out.println();
        Department.displayAverage();
    }
}

If you aren't allowed arguments in the constructor based on the diagram.如果您不允许在基于图表的构造函数中使用 arguments。 You could add the scanner code into the Department constructor.您可以将扫描器代码添加到 Department 构造函数中。 Then in the main create two different department objects.然后主要创建两个不同的部门对象。

public Department(){
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    // c,d,e etc...
    groupScore = a + b + c + d + e;
    groupAverage = groupScore / 5.0;

    overallScore = overallScore + groupScore;

    if (overallAverage == 0.0) {
        overallAverage = groupAverage;
    } else {
        overallAverage = (groupAverage + overallAverage) / 2.0;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Enter 5 rates for Department 1 (1-5)");
        Department department1 = new Department();
        System.out.println("Department 1 group average: " + department1.getGroupAverage());
        Department.displayAverage();
        System.out.println();

        System.out.println("Enter 5 rates for Department 2 (1-5)");
        Department department2 = new Department();
        System.out.println("Department 2 group average: " + department2.getGroupAverage());
        Department.displayAverage();
    }
}

If you are allowed arguments , You need to pass in your variables into your Department constructor while creating the object. First create a constructor that takes in 5 integer values如果允许 arguments ,则需要在创建 object 时将变量传递到 Department 构造函数中。首先创建一个构造函数,它接受 5 个 integer 值

public Department(int a, int b, int c, int d, int e){
    groupScore = a + b + c + d + e;
    groupAverage = (a + b + c + d + e) / 5;
}

Second pass the values to the constructor in the main method.其次将值传递给 main 方法中的构造函数。 Like this像这样

int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();
Department objdept = new Department(a,b,c,d,e);

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

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