0

我知道这可能是一个奇怪的问题,但我真的被困住了。我有两个类的简单程序。我需要将数组从 A 类传递到 B 类。我做到了,但我无法测试它,因为我不知道如何运行程序。当我点击运行时,只有一堂课开始了。我想测试整个程序,但找不到任何方法。是否有任何命令或内容说先运行 A 类,然后运行 ​​B 类?没有它,我无法测试 B 类,因为未加载 Array(A 类)中的值:/ 希望你明白我的意思。

我正在使用日食。

谢谢!

类标记计算器

import java.util.Scanner;

public class MarkCalculator {

public static int[] exam_grade = new int[6];
public static int[] coursework_grade = new int[6];
public static int[] coursework_weight = new int[2];
public static int[] module_points = new int[6];
public static String module_grade, holder;
public static int counter1 = 0, counter2 = 0;

public static void main(String[] args) {

Scanner input = new Scanner (System.in);

for (int i=0; i<3; i++){

    System.out.printf(i+1+". Modelue"+" Enter grade of exam:");

    while (!input.hasNextInt() ){

        System.out.printf("Enter only numbers! Enter grade of your exam: ");
        input.next();
    }

    exam_grade[i]=input.nextInt();

    System.out.printf(i+1+". Modelue"+" Enter grade of coursework:");

    while (!input.hasNextInt()){

        System.out.printf("Enter only numbers! Enter grade of your coursework: ");
        input.next();
    }

    coursework_grade[i]=input.nextInt(); 
}


computeMark(coursework_grade, exam_grade, module_points);


// calculate module grade
for(int i = 0 ;i < 3; i++){

    if (module_points[i] < 35){
        System.out.println(i+1+".Module: Fail");
    }

    else if (module_points[i] >= 35 && module_points[i] <= 40){
        System.out.println(i+1+".Module: Pass by compensation");
        counter1++;
    }

    else {
        System.out.println(i+1+".Module: Pass");
        counter2++;
    }


}

holder = computeResult(module_points, counter1,counter2, module_grade);
System.out.println("Your stage result is: "+ holder);


input.close();
}



public static int[] computeMark (int coursework_grade[], int exam_grade[], int module_points[]){

coursework_weight[0]= 50;
coursework_weight[1]= 50;

for(int i=0;i<3;i++)
{

    if (coursework_grade[i] < 35 || exam_grade[i] < 35){

        module_points[i]=(coursework_grade[i]*coursework_weight[0] + (exam_grade[i]*(100-coursework_weight[1])))/100;

        if (module_points[i] > 35){
            module_points[i] = 35; } 

        else {
            module_points[i] = 0;
        }

    }

    else {
        module_points[i]=((coursework_grade[i]*coursework_weight[0] + (exam_grade[i]*(100-coursework_weight[1])))/100); }


}

return module_points;       
}

public static String computeResult (int module_points[], int counter1, int                 counter2,     String module_grade ){

int sum = 0;
double average = 0;

for (int i = 0; i < 3; i++){

    sum = sum + module_points[i];
    average = sum / 3;
}

for (int i = 0; i < 3; i++){


    if (counter2 == 3){
        module_grade = "Pass";
    }

    else if (average >= 40 && counter1 <= 2) {
        module_grade = "Pass by compensation";
    }

    else {
        module_grade = "Fail";
    }



}

return module_grade;






}



}

班级学生图表

public class StudentChart {

public static void main(String[] args) {


    for (int i = 0; i < 3; i++){
        System.out.println(MarkCalculator.coursework_weight);
    }




}

}
4

2 回答 2

3

你只需要一种main方法。

class A {
    String s;

    public A(String s){
        this.s = s;
    }
}

public class B {
    public static void main(String[] args){
        A a = new A("Hello");
        System.out.println(a.s + " world!");
    }
}

class B will be the application program, the one with the main method. It will get values from class A. class A does not need to run for class B app to work, even though it uses values from class A.

于 2013-11-13T19:00:20.087 回答
1

You can have a method with a different name in another class, and call that method from your main method.

Do not call it public static void main though - that should only be used for standalone programs. If the method requires some other code to be run prior to it, it should not be the main method of a Java program.

于 2013-11-13T19:15:39.037 回答