Java验证基本计算器程序中的输入

时间:2014-10-20 16:54:44

标签: java arrays string calculator do-while

我正在寻找如何让程序正常运行的答案。我希望我的程序识别除双打之外的其他东西 - 例如,如果我放入 idk ,它会说, 这不是一个数字 而不是崩溃。

我的代码:

import java.util.*;

public class CalculatorMain {
    public static void main(String[] args) {

        FunctionMain action = new FunctionMain();

         Scanner input = new Scanner(System.in);

        double answer = 0;
        double inputNu1, inputNu2;
        char operator;
        boolean end = false;

     while (! end ) {
            System.out.print(">>>");

            inputNu1 = input.nextDouble();
            operator = input.next().charAt(0);
            inputNu2 = input.nextDouble();        

    switch (operator) 
            {
        case '-': answer = action.Subtract(inputNu1, inputNu2);
                break;
        case '*': answer = action.Multiply(inputNu1, inputNu2);
                break;
        case '+': answer = action.Add(inputNu1, inputNu2);
                break; 
        case '/': answer = action.Divide(inputNu1, inputNu2);
               break;          
            }

        System.out.println(answer);             
        }       
        input.close();

    }

}



public class FunctionMain {

     double Subtract (double Nu1 , double Nu2)
    {
         return Nu1 + Nu2;

    }
    double Multiply (double Nu1 , double Nu2)
    {
        return Nu1 * Nu2;
    }
    double Add (double Nu1 , double Nu2)

    {
        return Nu1  + Nu2;
    }
    double Divide (double Nu1 , double Nu2)

    {  
        return Nu1 / Nu2;
    }

}

3 个答案:

答案 0 :(得分:0)

当您尝试将String转换为Double时会出现异常,因此您必须按如下方式进行管理:

double n = 0.0;
try {
    n = input.nextDouble();
} catch (Exception e) {
    // there was an error, and here we can do whatever
    System.out.println( "It was not a number!" );
}

详细了解有关删除的信息:http://docs.oracle.com/javase/tutorial/essential/exceptions/

答案 1 :(得分:0)

类似的东西:

public static boolean isNumeric(String str)
{
  return str.matches("-?\\d+(\\.\\d+)?");  //match a number with optional '-' and decimal.
}

或:

public static boolean isNumeric(String str)  
{  
  try  
  {  
    double d = Double.parseDouble(str);  
  }  
  catch(NumberFormatException nfe)  
  {  
    return false;  
  }  
  return true;  
}

退房:How to check if a String is numeric in Java

答案 2 :(得分:0)

这里的主要想法是在检索到的令牌与预期类型不匹配时找到扫描程序引发的异常。如果在代码中输入了错误的输入,则会看到控制台中显示的java.util.InputMismatchException。因此,我们可以使用try{....}catch(java.util.InputMismatchException e){....}来捕获此特定异常并添加一些用户定义的操作。

我编辑了 CalculatorMain类,现在如果输入无法解析为双倍的输入,控制台会输出警告并提示您再次输入新输入。

**Sample console output:**
>>>jdk
java.util.InputMismatchException
jdk IS INVALID INPUT
>>>test
java.util.InputMismatchException
test IS INVALID INPUT
>>>input 10
*
789
7890.0
import java.util.*;
public class CalculatorMain {
public static void main(String[] args) {
    FunctionMain action = new FunctionMain();
    double answer = 0;
    double inputNu1, inputNu2;
    char operator;
    boolean end = false;
    Scanner input = new Scanner(System.in);
    while (! end ) {

        System.out.print(">>>");
        try{
            inputNu1 = input.nextDouble();
            operator = input.next().charAt(0);
            inputNu2 = input.nextDouble();   
            switch (operator){
            case '-': answer = action.Subtract(inputNu1, inputNu2);
                break;
            case '*': answer = action.Multiply(inputNu1, inputNu2);
                break;
            case '+': answer = action.Add(inputNu1, inputNu2);
                break; 
            case '/': answer = action.Divide(inputNu1, inputNu2);
                break;          
            }

            System.out.println(answer); 

        }catch(InputMismatchException e){
            System.out.println(e.toString());
            System.out.println(input.next() + " IS INVALID INPUT");
        }
    } 
    input.close();
    }
}

有关Java中例外的更多信息 - Catching and Handling ExceptionsClass InputMismatchException