我想知道是否可以通過將函數名稱添加到參數來調用另一個函數。因此,例如,我想用4個部分製作腳本。每個部分都需要輸入(我使用掃描儀,不要問爲什麼:P是它的任務),然後需要將其傳遞給另一個腳本,例如,計算和東西。通過將其添加到參數調用另一個函數
我這個開始的:
static int intKiezer(String returnFunctie, String text) {
Scanner vrager = new Scanner(System.in);
while (true) {
System.out.println(text);
int intGekozen = vrager.nextInt();
if (vrager.hasNextInt()) {
returnFunctie(intGekozen);
}
else {
vrager.next();
System.out.println("Verkeerde invoer!");
}
}
正如你看到的,我想通過努力把它(returnFunctie(intgekozen))所獲得的價值推到另一個功能。它應該用intgekozen作爲參數調用returnFunctie。但它不起作用
我會調用這樣的函數:intKiezer(sphereCalculations, "What radius do you want to have?")
。所以從輸入的答案,如果其正確的應該傳遞給另一個函數稱爲sphereCalculations
你的returnFunctie(...)代碼在哪裏? – kosa
原則上這可能與反思,但真正的問題是:你爲什麼想這樣做?這表明你的程序有一個奇怪的設計。 (反射只能用於特殊情況,我不想學習你的壞習慣)。 – Jesper
事情是我有多個輸入,我想爲他們做一個功能。不僅僅是說明掃描儀的每種功能,例如,這對我來說似乎是最簡單的方法,因爲我只需添加文本和函數名稱 –