2017-07-15 5 views
0

以下は年齢を計算するコードです。スイッチケースのメインメニューに戻る

ケース1を実行したときにメインメニュー:Select your choice:...をもう一度表示します。

私は休憩を与えていましたが、それは動作していません。

私のコンソールには、完了時にもう一度Enter your Birth date(DD):が表示されます。

表示したい:Select your choice.アイデア?

import java.util.Calendar; 
    import java.util.Scanner; 

    public class Switch { 
     public static void main(String args[]) 
     { 
      System.out.println("Select your choice"); 
      System.out.println("1. To Calculate your Age"); 
      System.out.println("2. Calculator"); 
      System.out.println("3. To Exit"); 
      boolean exit=false; 
      Scanner sc1=new Scanner(System.in); 
      int choice=sc1.nextInt(); 
      do{ 
       switch(choice) 
       { 
       case 1: 

        Calendar c1 = Calendar.getInstance(); 
        int currDate = c1.get(Calendar.DATE); 
        //System.out.println(currDate); 

        int currMonth = c1.get(Calendar.MONTH)+1; 
        //System.out.println(currMonth); 

        int currYear=c1.get(Calendar.YEAR); 
        //System.out.println(currYear); 
        Scanner sc2=new Scanner(System.in); 
        System.out.println("Enter your Birth date(DD): "); 
        int birthDate=sc2.nextInt(); 

        System.out.println("Enter your Birth month(MM): "); 
        int birthMonth=sc2.nextInt(); 

        System.out.println("Enter your Birth year(YYYY): "); 
        int birthYear=sc2.nextInt(); 
        int calculateDate=0; 


        calculateDate=currDate-birthDate; 

        int calculateMonth=currMonth-birthMonth; 
        int calculateYear=currYear-birthYear; 

        System.out.println("You are : "+ calculateDate+"days "+" "+calculateMonth+"months "+calculateYear+"years OLD"); 
        System.out.println("thank you.."); 
        System.out.println("\n"); 
        break; 
      case 2: 

        System.out.println("This is case 2"); 
        break; 

      case 3: 

        exit=true; 
        break; 

      } 
     }while(!exit); 
    } 
} 
+0

次のように{行う入れる(ループの中であなたのループ – khelwood

+0

入れて、プログラム全体の内側 'choice'を設定していませんあなたのメインメソッドの最初の行) – PerunSS

答えて

2

中にあなたの下の行を移動してください。

 System.out.println("Select your choice"); 
     System.out.println("1. To Calculate your Age"); 
     System.out.println("2. Calculator"); 
     System.out.println("3. To Exit"); 
     Scanner sc1 = new Scanner(System.in); 
     int choice = sc1.nextInt(); 

SwitchCase.java

import java.util.Calendar; 
import java.util.Scanner; 

public class SwitchCase { 
    public static void main(String args[]) { 
     boolean exit = false; 
     do { 
      System.out.println("Select your choice"); 
      System.out.println("1. To Calculate your Age"); 
      System.out.println("2. Calculator"); 
      System.out.println("3. To Exit"); 
      Scanner sc1 = new Scanner(System.in); 
      int choice = sc1.nextInt(); 
      switch (choice) { 
      case 1: 
       Calendar c1 = Calendar.getInstance(); 
       int currDate = c1.get(Calendar.DATE); 
       // System.out.println(currDate); 
       int currMonth = c1.get(Calendar.MONTH) + 1; 
       // System.out.println(currMonth); 
       int currYear = c1.get(Calendar.YEAR); 
       // System.out.println(currYear); 
       Scanner sc2 = new Scanner(System.in); 
       System.out.println("Enter your Birth date(DD): "); 
       int birthDate = sc2.nextInt(); 
       System.out.println("Enter your Birth month(MM): "); 
       int birthMonth = sc2.nextInt(); 
       System.out.println("Enter your Birth year(YYYY): "); 
       int birthYear = sc2.nextInt(); 
       int calculateDate = 0; 
       calculateDate = currDate - birthDate; 
       int calculateMonth = currMonth - birthMonth; 
       int calculateYear = currYear - birthYear; 
       System.out.println("You are : " + calculateDate + "days " + " " 
         + calculateMonth + "months " + calculateYear 
         + "years OLD"); 
       System.out.println("thank you.."); 
       System.out.println("\n"); 
       break; 
      case 2: 
       System.out.println("This is case 2"); 
       break; 
      case 3: 
       exit = true; 
       break; 
      } 
     } while (!exit); 
    } 
} 

実行サンプル

Select your choice 
1. To Calculate your Age 
2. Calculator 
3. To Exit 
1 
Enter your Birth date(DD): 
1 
Enter your Birth month(MM): 
1 
Enter your Birth year(YYYY): 
1920 
You are : 14days 6months 97years OLD 
thank you.. 


Select your choice 
1. To Calculate your Age 
2. Calculator 
3. To Exit 
+1

ああ!はい...正解。私はそれをするのを忘れました.BTWありがとう。 –

0

ケース1を実行すると、変数exitに "true"を割り当てます。

0
You have given option to select the choices outside the loop.The code for selecting the options must be inside loop. 
Below is the updated code which may work: 

import java.util.Calendar; 
import java.util.Scanner; 

import java.util.Calendar; 
import java.util.Scanner; 

public class Switch { 
    public static void main(String args[]) 
    { 
     boolean exit; 
     do{ 
       System.out.println("Select your choice"); 
       System.out.println("1. To Calculate your Age"); 
       System.out.println("2. Calculator"); 
       System.out.println("3. To Exit"); 
       exit=false; 
       Scanner sc1=new Scanner(System.in); 
       int choice=sc1.nextInt(); 
        switch(choice) 
        { 
        case 1: 

         Calendar c1 = Calendar.getInstance(); 
         int currDate = c1.get(Calendar.DATE); 
         //System.out.println(currDate); 

         int currMonth = c1.get(Calendar.MONTH)+1; 
         //System.out.println(currMonth); 

         int currYear=c1.get(Calendar.YEAR); 
         //System.out.println(currYear); 
         Scanner sc2=new Scanner(System.in); 
         System.out.println("Enter your Birth date(DD): "); 
         int birthDate=sc2.nextInt(); 

         System.out.println("Enter your Birth month(MM): "); 
         int birthMonth=sc2.nextInt(); 

         System.out.println("Enter your Birth year(YYYY): "); 
         int birthYear=sc2.nextInt(); 
         int calculateDate=0; 


         calculateDate=currDate-birthDate; 

         int calculateMonth=currMonth-birthMonth; 
         int calculateYear=currYear-birthYear; 

         System.out.println("You are : "+ calculateDate+"days "+" "+calculateMonth+"months "+calculateYear+"years OLD"); 
         System.out.println("thank you.."); 
         System.out.println("\n"); 
         break; 
       case 2: 

         System.out.println("This is case 2"); 
         break; 

       case 3: 

         exit=true; 
         break; 

       } 
      }while(!exit); 
    } 
} 
+0

はい!私はそれをするのを忘れてしまった...ありがとう。 –

関連する問題