参照于github的schisa的代码
import static java.lang.System.out;import java.util.InputMismatchException;import java.util.Scanner;public class Calculator {public static int menu(Scanner userInput) {int inputInt;do { // Loop until the user enters a value between 1 and 5try {// Get users input for menu choiceinputInt = userInput.nextInt();// Check to see to see if user inserted the correct valuesif (inputInt >= 1 && inputInt <= 5) {// Leave loop since the user used a correct valuebreak;}// Else statement to catch all integer inputs that are not a// menu choice.else {out.println("That is not a menu option. Please re-enter your choice: ");continue; // Not a valid}// Catch the exception if the user tries to input a non integer// value} catch (final InputMismatchException e) {out.println("You have entered an invalid choice, please re-enter your choice: ");// discard the invalid inputuserInput.nextLine();// Start loop again because the user needs to choose valid// inputscontinue;}// Infinite loop. User must stay in this method until they input a// correct value.} while (true);// Return the users menu option to main methodreturn inputInt;}// Method for setting the math operation string when the user is inputting// floats// For example, when a user decides to use addition it will ass the user// which floats they would like to addpublic static String mathOperationString1(int menuChoice) {// Create choice to return to main methodString choice = "";// Check if user inputs 5 to exit the program. Set stringswitch (menuChoice) {// Addition.case 1:choice = "add";// Leave switchbreak;// Subtractcase 2:choice = "subtract";// Leave switchbreak;// Multiplycase 3:choice = "multiply";// Leave switchbreak;// Dividecase 4:choice = "divide";// Leave switchbreak;// Exitcase 5: // Display goodbye messageout.println("Thanks for using Stephen Chisa's Handy Calculator");// Exit the programSystem.exit(0);}// Return string to mainreturn choice;}// Method for setting the math operation string in the final result// For example, when a user decides to use addition the final string will// include the word "adding".public static String mathOperationString2(int menuChoice) {// Create choice to return to main methodString choice = "";// Check if user inputs 5 to exit the program. Set stringswitch (menuChoice) {// Menu 1: addition.case 1:choice = "adding";// Leave switchbreak;// Menu 2: subtractcase 2:choice = "subtracting";// Leave switchbreak;// Menu 3: multiplycase 3:choice = "multiplying";// Leave switchbreak;// Menu 4: Dividecase 4:choice = "dividing";// Leave switchbreak;}// Return string to mainreturn choice;}// Method for user to input two float valuespublic static void getCalcFloats(int menuChoice, String mathString, float[] myFloats, Scanner userInput) {out.printf("Enter two floats to %s, separated by a space: ", mathString);do { // Loop until we have correct inputtry {// Get user input for two floatsmyFloats[0] = userInput.nextFloat();myFloats[1] = userInput.nextFloat();// check if the user is trying to divide by 0if (menuChoice == 4 && myFloats[1] == 0) {out.println("You cannot divide by zero please reenter both floats: ");// Start loop again because the user needs to choose valid// inputscontinue;}// leave the loop since the user provided valid inputbreak;}// Catch the exception if the user tries to insert a non-float valuecatch (final InputMismatchException e) {out.println("You have entered invalid floats please re-enter: ");// discard non-float valueuserInput.nextLine();// Start loop again because the user needs to choose valid// inputscontinue;}// Infinite loop. User must stay in this method until they input a// correct value.} while (true);}// Method for calculatingpublic static float calcValues(int menuOption, float number1, float number2) {float result = 0;// Start a switch based on the users menu optionswitch (menuOption) {// Additioncase 1: // Calculate First float + second floatresult = number1 + number2;break;// Subtractioncase 2: // Calculate first float - second floatresult = number1 - number2;break;// Multiplicationcase 3: // Calculate first float * second floatresult = number1 * number2;break;// Divisioncase 4: // Calculate result for number1/number2// I previously checked if number2 is 0result = number1 / number2;break;}// Return the calculated valuereturn result;}// Main Methodpublic static void main(String[] args) {// Begin do while loop so program keeps running until the user exits.do {// Welcome the user to the calculator. Display menu.out.println("Welcome to Stephen Chisa's Handy Calculator");out.print("\n\t1.Addition \n\t2.Subtraction \n\t3.Multiplication \n\t4.Division \n\t5.Exit \n\nWhat would you like to do? ");// Enable scannerScanner input = new Scanner(System.in);// Run the menu method and store the values in menuChoiceint menuChoice = menu(input);// Run method to get a string for "add", "subtract", "multiply" or// "divide"String mathFirstString = mathOperationString1(menuChoice);float[] calcFloats = new float[2];// Run the getTwoFloats method. Return the values into the myFloats// arraygetCalcFloats(menuChoice, mathFirstString, calcFloats, input);// Run method to get a string for "adding", "subtracting",// "multiplying" or "dividing"String mathSecondString = mathOperationString2(menuChoice);// Calculate the result of the inputsfloat Calculated = calcValues(menuChoice, calcFloats[0], calcFloats[1]);// Display the result of the calculationout.printf("Result of %s %.2f and %.2f is: %.2f.", mathSecondString, calcFloats[0], calcFloats[1],Calculated);// Press enter to continue messageout.println("\n\nPress enter key to continue...");input.nextLine();// Pause until user presses enterinput.nextLine();} while (true);}}