2016-04-23 12 views
-1

このコードを持つ方法を教えてもらえれば、4を押すとメインメニューに戻ることができます。私はループ中に行う必要があることを知っているが、私はどのようにするかわからない。今、私は戻り値として終了しているので、プログラム全体を閉じますが、代わりにメインメニューに戻り、eventSelectionを再起動します。exitをメインメニュー(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 1時間前。上記のリンクのコメントは、あなたが何をする必要があるかを既に示唆しています。なぜこれを再度投稿するのですか? – 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(再呼び出し)

関連する問題