2016-04-23 230 views
-1

有人可以告訴我如何讓這個代碼返回到主菜單時,按4。我知道我必須做循環,但我不知道如何。現在我退出作爲一個返回,所以它關閉了整個程序,但我希望它返回到主菜單並重新啓動eventSelection。如何退出返回到主菜單(Java)

import java.util.*; 
public class SchedulingProgram 
{ 
    public static void main (String [] args) 
    { 
    eventSelection(); 
    } 
    public static void eventSelection() 
{ 
Scanner sc = new Scanner(System.in); 
System.out.println("Select Scheduling Action"); 
System.out.print("Add and Event = 1. \nDisplay Events = 2. \nPrint Alert = 3. \nExit = 4. \nINPUT : "); 
int actionSelect = sc.nextInt(); 

if (actionSelect >= 1 && actionSelect <= 4) 
{ 
if (actionSelect == 1) 
    { 
    addEvent(); 
    } 
else if (actionSelect == 2) 
    { 
    displayEvent(); 
    } 
else if (actionSelect == 3) 
    { 
    printAlert(); 
    } 
else if (actionSelect == 4) 
    { 
    return; 
    } 
} 
else 
{ 
    System.out.println("Error : Choice " + actionSelect + " Does Not Exist."); 
} 
} 
+0

因此,如何用戶將退出你的程序? –

+3

你爲什麼一次又一次地提出同樣的問題?你只是問了這個問題:回到一小時後,http://stackoverflow.com/questions/36806696/how-to-return-to-main-menu-when-exit-is-inputted。上面鏈接中的評論已經提示你需要做什麼。爲什麼要再發布一次? – Madhusudhan

+0

@ user3493289指向此問題的鏈接 –

回答

0

你可以用這樣一個無限循環做到這一點,但用戶沒有選擇完全退出程序,如果你想有一個命令退出程序,你可以在命令中使用return

public static void eventSelection() 
{ 
    while (true){ 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Select Scheduling Action"); 
     System.out.print("Add and Event = 1. \nDisplay Events = 2. \nPrint Alert = 3. \nExit = 4. \nINPUT : "); 
     int actionSelect = sc.nextInt(); 

     if (actionSelect >= 1 && actionSelect <= 4) 
     { 
      if (actionSelect == 1) 
      { 
       addEvent(); 
      } 
      else if (actionSelect == 2) 
      { 
       displayEvent(); 
      } 
      else if (actionSelect == 3) 
      { 
       printAlert(); 
      } 
      else if (actionSelect == 4) 
      { 
       System.out.println("Returning to selection menu");// Do nothing, next loop will be executed 
      } 
     } 
     else 
     { 
      System.out.println("Error : Choice " + actionSelect + " Does Not Exist."); 
     } 

    } 

}

0

返回從eventSelection(一些值)。檢查的主要功能價值是什麼returned.If它與你的價值相匹配,然後再調用eventSelection()