2015-09-20 40 views
-3

主菜單:
1.Plus
2.Minus
3.Multiply
4.Division
5.Exit
選項:我必須在此後添加什麼之後添加什麼? System.out.println(「*按任意鍵回到菜單*」);

如果菜單1:
瓦爾答:... 5 < - 用戶條目
變量B:... 3 < - 用戶條目
結果:8
5> 3
按任意鍵回到菜單

我必須在此之後添加什麼?
System.out.println(「按任意鍵回到菜單」);

我很困惑,請任何人親可以幫我(T-T),非常感謝你!

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); 


    } 
} 

回答

0

剛剛的System.out.println( 「按任意鍵返回菜單」)後,添加entry.next();

+0

需要點擊任意鍵i型... – BeginnerToJava

+0

如何使它自動返回菜單無需使用鑰匙進入後我型 - 任意鍵後,進入後??? – BeginnerToJava

+0

我認爲你不能沒有本地代碼和JNI。看看Java Curses庫:http://sourceforge.net/projects/javacurses/ – pinokio

1

返回一個布爾變量並詢問用戶他是否想要返回。 使用continue語句來繼續進程並打破進程。在這裏我想給你一個明顯而簡短的例子。

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."); 
    } 
} 
+0

兄弟...如何做到這一點:(輸入B爲後退,任何其他退出鍵)自動輸入後不使用輸入鍵??? – BeginnerToJava

+0

然後你必須改變你的終端(控制檯)從線路模式(熟食模式,輸入密鑰是必需的)字符模式(原始模式,輸入密鑰不是必需的),但在Java的JDK它不可用。你可以使用[JCurses](sourceforge.net/projects/javacurses/)。 – CodeRunner

相關問題