2014-04-17 16 views
1

我是一個新的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或退出系統。

+3

分享您的代碼,以便此處的社區可以爲您提供幫助,並說明您想要的結果/輸出。 – Sky

+0

因此,您希望用戶在運行時輸入以選擇要執行的功能。你可以使用開關的組合,而 –

+0

是的,我會嘗試,欣賞! – xiaopassion

回答

1

您可以使用do while環和switch的組合基於用戶輸入

public class Test{ 

    public static void main(String args[]){ 
     Scanner sc = new Scanner(System.in); 
     int choice; 

     do{ 
     choice = sc.nextInt(); 
     switch(choice){ 

      case 1: function1(); 
        break; 
      case 2: function2(); 
        break; 
      case 3: function3(); 
        break; 
      case 4: function4(); 
        break; 

      } 

     }while(choice!=0); 
    } 

public static void function1(){/*definition*/} 
public static void function2(){/*definition*/} 
public static void function3(){/*definition*/} 
public static void function4(){/*definition*/} 

} 
+1

這是一個很好的提示,我非常感謝。 – xiaopassion

0

如果你想擁有的功能(方法)的集合,然後選擇要編程調用,您可以嘗試以下方法:

先用將用於執行功能的方法創建的接口。假設你的函數具有一個int參數,並且返回一個String

public interface IFunction { 
    public String execute(int parameter); 
} 

然後就可以通過匿名內部類指定IFunction定義具體功能:

IFunction f1 = new IFunction() { 

    @Override 
    public String execute(int parameter) { 
     return "Always this text"; 
    } 
}; 

IFunction f2 = new IFunction() { 

    @Override 
    public String execute(int parameter) { 
     return Integer.toString(parameter); 
    } 
}; 

List<IFunction> flist = new ArrayList<>(); 
flist.add(f1); 
flist.add(f2); 

int choice = 1; //This can be filled using user input; 

flist.get(choice).execute(3); 

通過使用陣列,則可以通過用戶直接輸入即可獲得。

+0

有沒有辦法避免數組?非常感謝你 – xiaopassion

+0

@xiaopassion你可以按照下面的答案建議使用一個開關,然後調用f1.execute(參數)或f2.execute(參數)。我使用的數組是一種向您展示如何使用函數作爲對象的方式。這樣,當你添加新的函數時,你不需要添加新的「if」或「switch」控制結構的分支。 –

+0

找到了你,其實我還沒有學過陣列,我會在學習之後嘗試這個,非常感謝你 – xiaopassion

0

使用做出決定「做,而」的重複,而條件匹配.. 「如果別人」用於選擇功能

package com.loknath.lab; 
import java.util.Scanner; 
public class Demo3 { 
     public static void main(String[] args) { 
    int a = 0; 
    Scanner keyboard = new Scanner(System.in); 

    do { 
     System.out.println("chosewhat you want"); 
     System.out.println("1 - F1"); 
     System.out.println("2 - F2"); 
     System.out.println("3 - F3"); 
     System.out.println("4 - F4"); 
     System.out.println("0 - exit"); 

     a = keyboard.nextInt(); 
     if (a == 1) 
      f1(); 
     else if (a == 2) 
      f2(); 
     else if (a == 3) 
      f3(); 
     else if (a == 4) 
      f4(); 
     else if ((a >= 5) || (a < 0)) 
      System.out.println("Invalid Choice: Please try again"); 
    } while (a != 0); 
    System.out.println("Done"); 
} 

public static void f1() { 
    System.out.println("F1"); 
} 

public static void f2() { 
    System.out.println("F2"); 
} 

public static void f3() { 
    System.out.println("F3"); 
} 

public static void f4() { 
    System.out.println("F4"); 
} 

} 
相關問題