2014-08-31 94 views
-3

這個類在類MAIN_PROGRAM中被調用來相應地執行方法'menu1'。如果用戶將3作爲輸入,則應將執行控制返回給MAIN_PROGRAM類的main_menu方法。如何將執行控制從一個類的方法返回到另一個類的主方法?

我試圖在case 3中使用System.exit(0),因爲我認爲它只會關閉程序estimate_of_order_of_magnitude而不是整個過程,但它不起作用。換句話說,如果情況3被執行,main_menu必須執行。

import java.util.*; 

import java.util.*; 
public class estimation_of_order_of_magnitude 
{ 

    void menu1() 
    { 
     Scanner scr=new Scanner(System.in); 
     System.out.println("enter"); 
     int n=scr.nextInt(); 
     System.out.println("enter 1 to calculate height of object"); 
     System.out.println("enter 2 to calculate length of object"); 
     System.out.println("enter 3 to return to main menu"); // to return to mainmenu of other class 
     System.out.println("enter your chice"); 
     while(true) 
     { 
     switch(n) 
     { 
     case 1:height_of_object(); 
     case 2:length_of_object(); 
     case 3:System.exit(0); 
     } 
     } 
    } 
} 

import java.util.*; 

class MAIN_PROGRAM 

{ 
void main_menu() 

    { 
estimation_of_order_of_magnitude obj=new estimation_of_order_of_magnitude(); 

    encoding_and_decoding object=new encoding_and_decoding(); 
    Scanner scr=new Scanner(System.in); 
    System.out.println("enter"); 
    int n1=scr.nextInt(); 
    System.out.println("enter 1 for estimation of order of magnitude"); 
    System.out.println("enter 2 for encoding and decoding"); 
    System.out.println("enter 3 to exit"); 
    System.out.println("enter your choice"); 
while(true) 

     { switch(n1) 
      { 
      case 1:obj.menu(); 
      case 2:object(); 
      case 3:System.exit(0); 
      } 

      } 
    } 
} 

回答

3
System.exit(0); 

這將退出整個程序。從函數返回到調用它的方法,請使用

return; 
相關問題