我是一個新的java精簡者。 現在,我在一個需要4個函數的項目上工作(如f1,f2,f3,f4) 每個函數完成後,我可以選擇下一個要調用的函數。 其實我已經完成了4個功能,但我不知道如何實現功能選擇(我嘗試使用嵌套while循環,但我搞砸了) 我會很感激,如果有人可以給我一些想法。 謝謝。如何使用循環來重複選擇函數
import java.util.*;
import java.io.*;
public class driver
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("chosewhat you want");
int a = keyboard.nextInt();
keyboard.nextLine();
ObjectOutputStream objOut = null;
ObjectInputStream objIn = null;
Pet p = null;
Person owner = null;
while (a==1)
{
try
{
objOut = new ObjectOutputStream(new FileOutputStream("pet.dat"));
owner = new Person();
p = new Mammal();
owner.getInput();
p.owner = owner;
p.getInput();
objOut.writeObject(p);
objOut.close();
a = keyboard.nextInt();
keyboard.nextLine();
}
catch (Exception e)
{
System.out.println("here!!!");
}
}
while (a == 2)
{
try
{
objIn = new ObjectInputStream(new FileInputStream("pet.dat"));
try
{
while(true)
{
p = (Pet)objIn.readObject();
System.out.println(p);
}
}
catch (EOFException e)
{
System.out.println("System ends");
}
}
catch (Exception e)
{
System.out.println("something in driver");
}
a = keyboard.nextInt();
keyboard.nextLine();
}
}
}
以上是我的代碼, 如果我按1,書寫功能已經被調用,如果我按2,閱讀功能函數被調用時,如果我進入3,功能3(我卻沒有添加)必須被調用,但是我不知道如何在每個函數完成後有選項選擇,比如在函數2完成後,我可以選擇執行函數1,函數2,函數3或退出系統。
分享您的代碼,以便此處的社區可以爲您提供幫助,並說明您想要的結果/輸出。 – Sky
因此,您希望用戶在運行時輸入以選擇要執行的功能。你可以使用開關的組合,而 –
是的,我會嘗試,欣賞! – xiaopassion