初学者Java计算器 - 如何忽略输入的字符串而不是崩溃代码?

时间:2017-11-02 19:04:26

标签: java

我在这里缺少一个基本的解决方案吗? 旁注,我并不特别想要一个超级先进的解决方案,因为我在大学的计算机科学课程的前8周,我觉得这是一个简单的我想念的东西或者我尚未学到的东西。

请关注我的问题,我知道代码可以更清洁我现在不想要如何提高效率的提示我知道有太多的重复。

import java.util.Scanner;

public class Calculator {


public static void main(String[] args) {

    /*Initializing a string which will be 
     *used in the future to determine the 
     *operator the user wants to use*/
    String operator = "z"; //Cant be A,S,M or D or the program would skip my while loop

    //Creating number variables which will initilised later by user input//
    int numOne;
    int numTwo;

    //Creating a boolean variable asking the user if they would like
    //to run another calculation and a String to store Yes or No
    boolean anotherCalc = true;
    String runAgain = "p";

    Scanner userOperator = new Scanner(System.in); 
    Scanner userNumbers = new Scanner(System.in);
    Scanner userAgain = new Scanner(System.in);

    while (anotherCalc == true) { //Containing the whole calculator in this while loop
                             //so it only stops when the user says they don't want another calc
        while (!operator.equalsIgnoreCase("A") && !operator.equalsIgnoreCase("S") 
                && !operator.equalsIgnoreCase("M") && !operator.equalsIgnoreCase("D") 
                && operator.equalsIgnoreCase("z")){
            System.out.println("What kind of calculation would you like to make?");
            System.out.println("Press A for Addition\nPress S for Substraction");
            System.out.println("Press M for Multiplication\nPress D for Division");
            operator = userOperator.next(); 
            } 

        System.out.println("\nThank you, now what numbers would you like to use?");
        System.out.println("Enter number 1:");
        String numError;
        numError = userNumbers.next();
        numOne = Integer.parseInt(numError);

        System.out.println("Enter number 2:");
        String numError2;
        numError2 = userNumbers.next();
        numTwo = Integer.parseInt(numError2);

        int answer;

        //Starting outer if statement containing nested if statements for
        //all 4 operators 

            //Addition if statement with nested if statement that won't
            //break the code if someone trys to add outside the int value range
        if (operator.equalsIgnoreCase("A")){
                    if (numOne > 1073741824 || numTwo > 1073741824){
                       long answerlong = (long) numOne + (long) numTwo;
                       System.out.print("\nYour answer is " + answerlong);
                    } else {answer = numOne + numTwo;
                       System.out.println("\nYour answer is " + answer);}

            //Subtraction if statement with nested if statement that won't
            //break the code if someone trys to subtract from a number above the int value range 
        } else if (operator.equalsIgnoreCase("S")){
                    if (numOne > 2147483647 || numTwo > 2147483647){
                        long answerlong = (long) numOne - (long) numTwo;
                        System.out.print("\nYour answer is " + answerlong);
                    } else {
                        answer = numOne - numTwo;
                        System.out.println("\nYour answer is " + answer);
                    }


        } else if (operator.equalsIgnoreCase("M")){
                    if (numOne > 20000 || numTwo > 20000){
                       long answerlong = (long) numOne * (long) numTwo;
                       System.out.print("\nYour answer is " + answerlong);
                    } else {
                       answer = numOne * numTwo;
                       System.out.println("\nYour answer is " + answer); 
                    }


        } else if (operator.equalsIgnoreCase("D")){
                    if (numOne % numTwo == 0){                              //Nested if else statement so
                    answer = numOne / numTwo;                               //an answer with no decimal    
                    System.out.println("\nYour answer is " + answer);    //point is shown as a whole 
                                                                           //number and otherwise return decimals
                   } else {                                     
                   float answerdec = (float) numOne / (float) numTwo; 
                    System.out.printf("\nYour answer is " + "%.2f",answerdec);} 


        } else {
            System.out.println("\nError");
        }

        System.out.println("\nWould you like to run another calculation?");
        System.out.println("Press Y for Yes or N for No");
        runAgain = userAgain.next();

        if (runAgain.equalsIgnoreCase("Y")){
            anotherCalc = true;
            operator = "z";     //This needs to be added so it returns to ask the user what type of calculation
            System.out.println("\n\n"); //This is just for aesthetics, creating 2 lines before the program runs again. Not included on the first run of the program on purpose. 
        } else if (runAgain.equalsIgnoreCase("N")){
            anotherCalc = false;
        } else {
            anotherCalc = false;
            System.out.println("\n\nInput Error (Y or N not entered)\nPlease reload the program");
        }

    }    
        System.out.println("\n\nThank you for using the calculator");
        System.out.println("I hope you enjoyed it");

1 个答案:

答案 0 :(得分:1)

parseInt()将抛出NumberFormatException,因此它应该被捕获...例如:

    try{
        numOne = Integer.parseInt(numError);
    }catch(NumberFormatException nfe){
        System.out.println("Error:  value entered is not a number");
        System.out.println("Please try again");
        continue;
    }