2017-09-16 58 views
-3

我正在为一所学校的项目工作。在这一点上,我只是想过度,如果以下任何一个条件都不是真的,我想运行班级bookstoreCreditPersonal,但是我不能让它工作。有什么建议么?如何从另一个类运行Java类?

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); 
    } 
} 
+0

看看[问] – pvg

+1

你不能跑班。您可以实例化一个类的对象,或者可以在该类的对象上调用该类或实例方法的静态方法。 – Palle

+0

那么,当用户输入错误时,你基本上想要从顶部再次执行'main'? –

回答

1

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

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

另外,你的类名应该总是以大写字母开头:RunClassBSCP,而不是'runClassBSCP”。欲了解更多信息,请查看Code Conventions for the Java Programming Language