-3

Main Menu :
1.Plus
2.Minus
3.Multiply
4.Division
5.Exit
Option :

If Menu 1 :
Var A : … 5 <- User Entry
Var B : … 3 <- User Entry
Result : 8
5 > 3
press any key back to menu

what must i add after this???
System.out.println("press any key back to menu");

i am so confuse please anyone pro to java help me (T-T),thank you very much!!!

import java.util.Scanner;
class case2 {
   public static void main(String args[]) {
   Scanner entry = new Scanner(System.in);
   int a, b;
   int i;

do {
   System.out.println("Main Menu :");
   System.out.println("1.   Plus");
   System.out.println("2.   Minus");
   System.out.println("3.   Multiply");
   System.out.println("4.   Division");
   System.out.println("5.   Exit");
   System.out.println("Option :");

    i = entry.nextInt(); 
    System.out.println(" ");  

    switch(i) {
       case 1 :
         System.out.println("Menu 1 :");
         System.out.print("Var A :");
         a = entry.nextInt();
         System.out.print("Var B :");
         b = entry.nextInt();
         System.out.println("Result :" + (a+b));
         if(a>b) {
           System.out.println(a +" > "+b);
         }
         else if(a<b) {
           System.out.println(a +" < "+b);
         }
         else {
           System.out.println(a +" = "+b);
         }
         System.out.println("*press any key back to menu*");

         break;
       case 2 :
         System.out.println("Menu 2 :");
         System.out.print("Var A :");
         a = entry.nextInt();
         System.out.print("Var B :");
         b = entry.nextInt();
         System.out.println("Result :" + (a-b));
         if(a>b) {
           System.out.println(a +" > "+b);
         }
         else if(a<b) {
           System.out.println(a +" < "+b);
         }
         else {
           System.out.println(a +" = "+b);
         }
         System.out.println("*press any key back to menu*");

         break;
       case 3 :
         System.out.println("Menu 3 :");
         System.out.print("Var A :");
         a = entry.nextInt();
         System.out.print("Var B :");
         b = entry.nextInt();
         System.out.println("Result :" + (a*b));
         if(a>b) {
           System.out.println(a +" > "+b);
         }
         else if(a<b) {
           System.out.println(a +" < "+b);
         }
         else {
           System.out.println(a +" = "+b);
         }
         System.out.println("*press any key back to menu*");

         break;

       case 4 :
         System.out.println("Menu 4 :");
         System.out.print("Var A :");
         a = entry.nextInt();
         System.out.print("Var B :");
         b = entry.nextInt();
         System.out.println("Result :" + (a/b));
         if(a>b) {
           System.out.println(a +" > "+b);
         }
         else if(a<b) {
           System.out.println(a +" < "+b);
         }
         else {
           System.out.println(a +" = "+b);
         }
         System.out.println("*press any key back to menu*");

         break;
           case 5:
                 break;
     default:
         System.out.println("ERROR");
         System.out.println("*press any key back to menu*");        



        }
  }while(i != 5);


  }
}
Colonel Thirty Two
  • 18,351
  • 7
  • 32
  • 69
Asker
  • 109
  • 7

2 Answers2

1

Introduce a boolean variable back and ask the user if he wants to go back. Use continue statement to continue the process and break for breaking. Here I want to give you an obvious and short example.

import java.util.Scanner;

class case2 {

    public static void main(String args[]) {
        Scanner entry = new Scanner(System.in);
        int i = 0;
        boolean back = true;
        do {
            if (!back) {
                break;
            }
            System.out.println("Press 1 for additoin.");
            System.out.println("Press 2 for subtraction.");
            i = entry.nextInt();
            switch (i) {
                case 1:
                    System.out.println("Good you entered 1.");
                    continue;
                case 2:
                    System.out.println("Good you entered 2.");
                    continue;
                default:
                    System.out.println("It occured an error");
                    System.out.println("Do you want to go back? Enter b for back, any other key for exit.");
                    if (entry.next().equals("b")) {
                        continue;
                    } else {
                        back = false;
                        break;
                    }
            }
        } while (i != 3);
        System.out.println("Program ended.");
    }
}
CodeRunner
  • 647
  • 5
  • 12
  • bro...how to make this : (Enter b for back, any other key for exit.) automatically without using enter after typing any key??? – Asker Sep 20 '15 at 06:17
  • Then you have to change your Terminal (Console) from line mode (cooked mode, enter key is required) to character mode (raw mode, , enter key isn't required) but in java's jdk it isn't available. You may use [JCurses](sourceforge.net/projects/javacurses/). – CodeRunner Sep 20 '15 at 08:27
0

just add entry.next() after System.out.println("press any key back to menu");

pinokio
  • 97
  • 4