简体   繁体   English

Java-菜单循环和数学计算

[英]Java - menu loop and math calculations

I'm working on a program that will give the user a menu, allow them to select an option, then ask for different numbers to do a calculation based on their menu choice. 我正在开发一个程序,该程序将为用户提供菜单,允许他们选择一个选项,然后根据他们的菜单选择要求不同的数字进行计算。 I've started with the basic outline of the program, but I need help getting loops implemented and working. 我已经从程序的基本概述开始,但是我需要帮助来实现循环并正常工作。

Here is my code so far: 到目前为止,这是我的代码:

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;

        } }


}
}

The first problem I'm having is getting the menu to loop the right way. 我遇到的第一个问题是使菜单以正确的方式循环播放。 it either doesn't work, or loops forever. 它要么不起作用,要么永远循环。

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

Get rid of that semi-colon. 摆脱那个分号。 When your choice satisfies the condition, it loops forever. 当您的选择满足条件时,它将永远循环。 When it doesn't, it will quit immediately (since none of the cases match) after printing out Invalid menu choice, please try again. 否则,将在打印出Invalid menu choice, please try again.后立即退出(因为没有匹配的情况) Invalid menu choice, please try again. .

Try this, it is somewhat more modular than yours but about the same ... 尝试一下,它比您的模块更具模块化,但大致相同...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM