2017-09-16 57 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