你如何从另一个类运行Java类?

时间:2017-09-16 03:22:40

标签: java

我正在为学校做一个项目。在这一点上我刚刚过去,我想运行类bookstoreCreditPersonal如果以下条件都不正确,但我不能让它工作。有什么建议吗?

import java.util.Scanner;
public class bookstoreCreditPersonal {
    public static void main(Object o) {
        String studentNamePers;
        String userType;
    double studentGPAPers;
    double bookstoreCreditPers;

    Scanner input = new Scanner(System.in);
    System.out.print("Please enter 'S' if you are the student, 'T' if you are the teacher, or 'P' if you are the Parent: ");
    userType = input.nextLine();

    if (userType.equals("S")) {
        System.out.println("Greetings student...");
        Scanner Sinput = new Scanner(System.in);
        System.out.println("Please enter your(The students) first and last name :");
        studentNamePers = input.nextLine();

        Scanner SSinput = new Scanner(System.in);
        System.out.println("Please enter your(The student's) GPA :");
        studentGPAPers = input.nextDouble();

        bookstoreCreditPers = studentGPAPers * 10;

        System.out.println(studentNamePers + ", your GPA is " + studentGPAPers + ", and you have an available bookstore credit of $" + bookstoreCreditPers);
    } else if (userType.equals("T")) {
        System.out.println("Teacher");
    } else if (userType.equals("P")) {
        System.out.println("Parent");
    } else {
        System.out.println("Lets try that again, one character, in capital form only please.");
        //created a class that reruns this class
        runClassBSCP.call(null);
    }
}

}

这是类runClassBSCP:

public class runClassBSCP {
    public void call() {
         bookstoreCreditPersonal.main(null);
    }
}

1 个答案:

答案 0 :(得分:1)

您需要实例化/创建类的对象。然后你可以用对象调用所需的方法。

runClassBSCP bscp = new runClassBSCP();
bscp.call();

此外,您的类名应始终以大写字母开头:RunClassBSCP,而不是`runClassBSCP'。有关详细信息,请查看Code Conventions for the Java Programming Language