2017-09-22 39 views
0

我有一個類的方法調用位於另一個類中的方法,該方法有切換的情況。問題是我無法從開關盒的方法中退出,並返回到調用它的方法後面的下一行。我已經搜索了StackOverFlow的類似問題。我也嘗試在這些答案中使用建議的解決方案來解決與退出開關案例有關的問題(使用條件,使用返回等)。不幸的是,當我使用這些解決方案時,我不會回到調用switch case方法的方法中的下一行。相反,我正在以「構建成功的消息」退出整個程序。退出一個方法,在一個類中的開關大小寫方法,在另一個類中調用它的方法

不是太抽象,我希望我不會發布一些模擬我面臨的實際問題的類。很抱歉,如果代碼太長。

public class TestClass { 
    ClassWithriginalMethod test; 

    public static void main(String[] args) { 
     ClassWithriginalMethod g = new ClassWithriginalMethod(); 
     g.presentMenuOptions(); 
    } 

} 

該類包含主要方法。

下一個類是有一個調用帶開關的情況下,方法的方法之一:

import java.util.ArrayList; 
import java.util.Scanner; 


public class ClassWithriginalMethod { 
    private final ArrayList<ClassWithSwitchCases> arr; 
    Scanner s = new Scanner(System.in); 

    public void presentMenuOptions() { 
     System.out.println(
     "_____________________________________________________________________________\n" 
     + "This Menu contains the following options:\n" 
     + "Please choose a number corresponding to your option\n" 
     + "1: to get create submenu\n" 
     + "2: to get edit sub menu\n" 
     + "3: to get view sub menu\n" 
     + "4: to get delete sub\n" 
     + "5: to exit this operation\n" 
     + "_____________________________________________________________________________\n"); 
     String str= s.nextLine();  
      switch (str) { 
       case "1": 
        System.out.println("Entering creation..."); 
        this.createMenu();//This method is working properly and user is moved to nextline, i.e shown the presentMenuOptions(). 
        break; 
       case "2": 
        System.out.println("Entering editing..."); 
        /* The below method is the damn method that calls the other class methods with swith cases.*/ 
        this.editMenu(); 
        /* 
        ** What I want is to reach the next methos below this comment when I get back from the switch case. 
        */ 
        System.out.println("We've exited from the othe class method with switch cases..."); 
        this.presentMenuOptions(); 
        break; 
       case "3": 
        System.out.println("Entering viewing..."); 
        this.viewMenu(); 
        this.presentMenuOptions(); 
        break; 
       case "4": 
        System.out.println("Entering deletion..."); 
        this.deleteMenu(); 
        this.presentMenuOptions(); 
        break; 
       default: 
        System.exit(0); 
      } 
    } 

    public ClassWithriginalMethod() { 
     this.arr = new ArrayList<>(0); 
    } 

    private void createMenu() { 
     ClassWithSwitchCases toBeCreated = new ClassWithSwitchCases(); 
     this.arr.add(toBeCreated); 
     this.checkingArraySize(); 

     this.presentMenuOptions(); 
    } 

    private void editMenu() { 
     this.checkingArraySize(); 
     System.out.println("The following objects are available. Please select the object with the corresponding index\n"); 
     this.arr.forEach(p -> System.out.printf("%-15d\t%-15s\t%-15s\n", arr.indexOf(p), p.getfName(),p.getsName())); // we print the array to see the indices and object main elems. 
     int i = s.nextInt(); 
     ClassWithSwitchCases toBeEdited = this.arr.get(i); //supposedly I am checking through another function if the object of index i is in the array. 
     toBeEdited.edit(toBeEdited); // it is here where we are calling the switch method in the other class 
     //this.presentMenuOptions(); 
    } 

    private void viewMenu() { 
     this.checkingArraySize(); 
     System.out.println("The following objects are available. Please select the object with the corresponding index"); 
     this.arr.forEach(p -> System.out.printf("%-15d\t%-15s\t%-15s\n", arr.indexOf(p), p.getfName(),p.getsName())); // we print the array to see the indices and object main elems. 
     int i = s.nextInt(); 
     ClassWithSwitchCases toBeViewed = this.arr.get(i); //supposedly I am checking through another function if the provided number id less than size of List. 
     toBeViewed.view(toBeViewed); // making this class calling the function in the other class 
     //this.presentMenuOptions(); 
    } 

    private void deleteMenu() { 
     this.checkingArraySize(); 
     System.out.println("The following objects are available. Please select the object with the corresponding index"); 
     int i = s.nextInt(); 
     ClassWithSwitchCases deleted = this.arr.get(i); //supposedly I am checking through another function if the provided number id less than size of List. 
     deleted.view(deleted); // making this class calling the function in the other class 
     //this.presentMenuOptions(); 
    } 

    private void checkingArraySize() { 
     if (this.arr.size() <= 0) {System.out.println("There are no objects in the aray");} 
     else { 
      arr.stream().map((p) -> { 
       System.out.println("The following objects are available."); 
       return p; 
      }).forEachOrdered((p) -> { 
       System.out.printf("%-15s\t%-15s\t%-15s\n", "index", "fName", "sName"); 
       System.out.printf("_____________________________________________________________________________\n"); 
       System.out.printf("%-15d\t%-15s\t%-15s\n", arr.indexOf(p), p.getfName(),p.getsName()); 
      }); 
     } 
    } 
} 

最後一類是一個帶開關的情況:

public class ClassWithSwitchCases { 

    private String fName; 
    private String sName; 

    Scanner s = new Scanner(System.in); 

    public ClassWithSwitchCases() { 
     System.out.println("Please enter first name"); 
     this.fName = s.nextLine(); 

     System.out.println("Please enter sur name"); 
     this.sName = s.nextLine(); 
    } 

    public String getfName() { 
     return fName; 
    } 
    public void setfName(String fName) { 
     System.out.println("Please enter first name"); 
     this.fName = fName; 
    } 
    public String getsName() { 
     return sName; 
    } 
    public void setsName(String sName) { 
     System.out.println("Please enter sur name"); 
     this.sName = sName; 
    } 
    public void edit(ClassWithSwitchCases o) { 
     System.out.println(
     "_____________________________________________________________________________\n" 
     + "The Edit Menu contains the following options:\n" 
     + "Please choose a number corresponding to your option\n" 
     + "1: to edit the object's first name\n" 
     + "2: to edit the object's sur name\n" 
     + "3: to exit this menu\n" 
     + "_____________________________________________________________________________\n"); 

     do { 
     switch (s.nextLine()) { 

      case "1": 
       o.setfName(s.nextLine()); 
       System.out.println(o.toString());// just to check if editing took place 
       this.edit(o); // put so that we can make other edits. 
       break; 
      case "2": 
       o.setsName(s.nextLine()); 
       System.out.println(o.toString());// just to check if editing took place 
       this.edit(o); 
       break; 
      case "3": 
       System.out.println("We are leaving the method with switch cases..."); 
       break; 
      default: 
       System.out.println("We are also leaving the method with switch cases..."); 
       break; 
     } 
      } while ((Integer.getInteger(s.nextLine()) <= 3) && (Integer.getInteger(s.nextLine()) > 0)); 
    } 
    public void view(ClassWithSwitchCases o) { 
     System.out.println(o.toString()); 
    } 

    @Override 
    public String toString() { 
     return "_____________________________________________________________________________\n" 
       + "First Name:" + this.getfName() + "\n" 
       + "Middle Name:" + this.getsName() + "\n" 
       + "_____________________________________________________________________________\n"; 

    } 
} 

如果您嘗試工作這些類,你會注意到:

  1. 我能夠執行createMenu()方法,然後我得到禮物MenuOptions()方法,應該是。
  2. viewMenu()deleteMenu()方法做他們的工作,但他們退出整個程序。
  3. editMenu()方法給了我nullPointerExeption,但我不知道那是哪個指針。
  4. 我在代碼的評論中指出了我的想法。
  5. 我添加了額外的System.out.println()消息,作爲調試我的代碼的一種方式。由於我只是一個初學者,所以我現在可以在這個階段進行。

如果從我的問題中有任何常規值,它是:如何退出方法與開關情況下,並返回到另一種方法,不一定是主要方法。

非常感謝您的幫助和耐心:)

+0

'return',也許? –

+0

@Joe C:在他們使用條件的答案之一中,他們指示要求刪除任何返回語句的人,並使用break。 –

回答

0

您可以在任何地方切換使用return語句。很可能,您想要將您的break語句更改爲return語句。

+0

我剛剛做了重新編譯,我只能退出整個程序。我得到的消息是: BUILD SUCCESSFUL(總時間:42秒) 這不是我想要的。我想回到我的presentMenuOptions()方法。 例如,當我想要退出整個程序時,我可以使用System.exit(0)。 –

+0

它是否輸入了任何switch子句 - 或者它是否進入默認設置 - 這是system.exit()? –

+0

你可以在這裏得到很多代碼... –

0

在我看來,雖然我的問題是有效的,但它不是核心問題。核心問題是我如何設想我的類(對象)以及它們是如何相互關聯的。換句話說,由於我編排解決方案的方式,我遇到了這個問題。如果我選擇另一種編排(即更合適的設計模式),我很可能不會面對這個問題。

在我看來,初學者(我是他們中的一員)認真構建一個大的解決方案將面臨這種「問題模式」(問無論是愚蠢的問題,還是迴避問題,如「你爲什麼這樣做?你想達到什麼目的?「因爲他們很快就會執行某些課程,但他們還沒有弄清楚解決方案的整體結構,行爲和創建方面。

雖然有趣的是,通過犯這些錯誤,他們學習。

感謝那些回答我的人和那些會回答或評論的人。

相關問題