2015-07-20 82 views
0

我正在開發一個程序,該程序將爲用戶提供一個菜單,允許他們選擇一個選項,然後根據菜單選擇要求不同的數字進行計算。我已經開始了這個程序的基本概述,但是我需要幫助獲得循環的實現和工作。Java - 菜單循環和數學計算

這裏是我到目前爲止的代碼:

import java.util.Scanner; 

public class LB04Alexander { 

public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.println("This program will perform several types of calculations."); 
    System.out.println(); 
    System.out.println("-MENU-"); 
    System.out.println("Select from the following menu options:"); 
    System.out.println("1 - Compute integer powers of a number"); 
    System.out.println("2 - Compute a factorial"); 
    System.out.println("3 - Compute a square root"); 
    System.out.println("4 - Compute a cube root"); 
    System.out.println("5 - Compute an exponential"); 
    System.out.println("6 - Compute the sine of an angle"); 
    System.out.println("7 - Compute the cosine of an angle"); 
    System.out.println("8 - Convert a positive integer to binary"); 
    System.out.println("9 - Exit"); 
    System.out.println("Please enter the number corresponding to your choice:"); 

    int menuChoice = input.nextInt(); 
    while (menuChoice >= 1 && menuChoice <= 9); { 
     switch (menuChoice) { 
     case 1: 
      System.out.println("You have chosen to compute integer powers of a number."); 
      System.out.println("Please enter any number for the base:"); 
      double base1 = input.nextDouble(); 
      System.out.println("Now, enter a non-negative integer for the exponent:"); 
      int exponent1 = input.nextInt(); 
      if (exponent1 < 0) 
       System.out.println("Error: exponent is negitive."); 
      else 
       System.out.println(base1 + "^" + exponent1 + " = " + ""); 
      break; 
     case 2: 
      System.out.println("You have chosen to compute a factorial (n!) of a non-negative integer."); 
      System.out.println("Please enter a non-negitive integer for n:"); 
      long fact = input.nextLong(); 
      int result = 1; 
      if (fact < 0) 
       System.out.println("Error: integer is negitive."); 
      else if (fact >= 127) 
       System.out.println("Error: Answer too large to calculate"); 
      else 
       for (int i = 1; i <= fact; i++) { 
        result = result * i; 
       } 
      System.out.println(fact + "! = " + result); 
      break; 
     case 3: 
      System.out.println("You have chosen to compute a square root."); 
      break; 
     case 4: 
      System.out.println("You have chosen to compute a cube root."); 
      break; 
     case 5: 
      System.out.println("You have chosen to compute an exponential."); 
      break; 
     case 6: 
      System.out.println("You have chosen to compute the sine of an angle."); 
      break; 
     case 7: 
      System.out.println("You have chosen to compute the cosine of an angle."); 
      break; 
     case 8: 
      System.out.println("You have chosen to convert a positive integer to binary"); 
      break; 
     case 9: 
      System.out.println("You have chosen to exit"); 
      System.out.println("Program LB04Alexander is now terminating..."); 
      input.close(); 
      System.out.println("The program has ended. Goodbye."); 
      break; 
     default: 
      System.out.println("Invalid menu choice, please try again."); 
      break; 

     } } 


} 
} 

我有越來越菜單循環正道的第一個問題。它要麼不工作,要麼永遠循環。

+0

目前,該程序接受許多,但什麼都不做。如果我通過'while(menuChoice> = 1 && menuChoice <= 9)將while循環更改爲do-while,'最後,如果有效,但會一遍又一遍地循環選擇的案例。 – Gregory

回答

3
while (menuChoice >= 1 && menuChoice <= 9); 

擺脫那個分號。當您的選擇滿足條件時,它會永久循環。當它沒有時,它將立即退出(因爲沒有任何匹配的情況)打印出來後Invalid menu choice, please try again.

+0

@RealSkeptic哎呀,太快地掠過OP的代碼,甚至沒有看到默認選擇。 –

+0

我拿出了幫助的分號。一旦計算成功,我希望程序直接回到主菜單 – Gregory

+0

你*可以*打破標籤,但這是一種醜陋。更好的方法是以不同的方式組織代碼。也許每個「菜單」可能是它自己的方法,從'main'調用。當用戶選擇返回到上一個菜單時,您只需從方法返回即可。 –

0

試試這個,它比你的,但差不多某種程度上更模塊化...

import java.util.Scanner; 

    public class LB04Alexander { 

     public static final Scanner SCANNER = new Scanner(System.in); 

     public static void main(String[] args) { 
     int choice; 

     showMenu(); 
     while((choice = getInt("Please enter the number corresponding to your choice:")) != 9) { 
      doCalculation(choice); 
      showMenu(); 
     } 
     } 

     public static void showMenu() { 
     System.out.println("This program will preform several types on calculations."); 
     System.out.println(); 
     System.out.println("===============MENU====================="); 
     System.out.println("Select from the following menu options:"); 
     System.out.println("1 - Compute integer powers of a number"); 
     System.out.println("2 - Compute a factorial"); 
     System.out.println("3 - Compute a square root"); 
     System.out.println("4 - Compute a cube root"); 
     System.out.println("5 - Compute an exponential"); 
     System.out.println("6 - Compute the sine of an angle"); 
     System.out.println("7 - Compute the cosine of an angle"); 
     System.out.println("8 - Convert a positive integer to binary"); 
     System.out.println("9 - Exit"); 
     System.out.println("========================================"); 

     } 


     private static void doCalculation(int choice) { 
     switch (choice) { 
     case 1: 
      System.out.println("\nYou have chosen to compute integer powers of a number."); 
      double base1 = getDouble("Please enter the number corresponding to your choice:"); 
      int exponent1 = getInt("Now, enter a non-negative integer for the exponent:"); 
      System.out.println(base1 + "^" + exponent1 + " = " + ""); 
      break; 
     case 2: 
      System.out.println("You have chosen to compute a factorial (n!) of a non-negative integer."); 
      long fact = getLong("Please enter a non-negitive integer for n:"); 
      int result = 1; 
      if (fact < 0) 
      System.out.println("Error: integer is negitive."); 
      else if (fact >= 127) 
      System.out.println("Error: Answer too large to calculate"); 
      else 
      for (int i = 1; i <= fact; i++) { 
       result = result * i; 
      } 
      System.out.println(fact + "! = " + result); 
      break; 
     case 3: 
      System.out.println("You have chosen to compute a square root."); 
      break; 
     case 4: 
      System.out.println("You have chosen to compute a cube root."); 
      break; 
     case 5: 
      System.out.println("You have chosen to compute an exponential."); 
      break; 
     case 6: 
      System.out.println("You have chosen to compute the sine of an angle."); 
      break; 
     case 7: 
      System.out.println("You have chosen to compute the cosine of an angle."); 
      break; 
     case 8: 
      System.out.println("You have chosen to convert a positive integer to binary"); 
      break; 
     default: 
      System.out.println("Invalid menu choice, please try again."); 
      break; 
     } 
     } 

     private static int getInt(String prompt) { 
     boolean unassigned = true; 
     int value = 0; 
     while(unassigned) { 
      try { 
       System.out.println(prompt); 
       value = SCANNER.nextInt(); 
       unassigned = false; 
      } 
      catch (IllegalArgumentException e) {} 
     } 
      System.out.println("[" + value + "]"); 

     return value; 
     } 

     private static double getDouble(String prompt) { 
     boolean unassigned = true; 
     double value = 0; 
     while(unassigned) { 
      try { 
       System.out.println(prompt); 
       value = SCANNER.nextDouble(); 
       unassigned = false; 
      } 
      catch (IllegalArgumentException e) {} 
     } 
      System.out.println("[" + value + "]"); 
     return value; 
     } 

     private static long getLong(String prompt) { 
     boolean unassigned = true; 
     long value = 0; 
     while(unassigned) { 
      try { 
       System.out.println(prompt); 
       value = SCANNER.nextLong(); 
       unassigned = false; 
      } 
      catch (IllegalArgumentException e) {} 
     } 
      System.out.println("[" + value + "]"); 

     return value; 
     } 
    }