尝试并捕获我的公共空

时间:2017-02-10 22:57:33

标签: java exception-handling try-catch

在你问之前,我已经看过其他问题了,它们似乎只是略有不同。 我是java新手并且正在学习,所以我制作了这个自恋程序,我将把它变成一个多工具类的东西。那部分并不重要。 基本上,我正在制作一个文本输入计算器,您可以在其中键入数字,操作和内容,但这很简单。但是,在输入区域中,我正在尝试捕捉,以防用户键入不是数字的内容。但是,这使得x和y的变量(用户输入)在初始化单独的 public void calculator(变量)时未初始化且无法读取。这是我的代码(我只包括计算器部分,其余部分不相关,所有工作正常,我也知道 x 处理不同y,我正在测试它们)

        public void calculatorvariables() {
    System.out.println("Please enter the first number in your calculation.");
    double x = scanner.nextDouble();
    if(x != (double) x) {
        System.out.println("An error occurred! Did you input nothing or something other than a number? Returning to variable input screen!");
    } else
    System.out.println("Please enter the operator. Valid operators are: '+', '-', '*', '/'");
    String operation = scanner.next();
    System.out.println("Please enter the second number in your calculation.");
    try {
    double y = scanner.nextDouble();
    } catch (NumberFormatException e) {
        System.out.println("An error occurred! Error message: " + e.getMessage() + " Did you input nothing or something other than a number? Returning to variable input screen!");
    }
    new FirstClass().calculator(x, operation, y);
}
public void calculator(double x, String operation, double y) {
    if(operation.equals("+")) {
        System.out.println(x + y);
    }
    else if(operation.equals("-")) {
        System.out.println(x - y);
    }
    else if(operation.equals("*")) {
        System.out.println(x * y);
    }
    else if(operation.equals("/")) {
        System.out.println(x / y);
    }
    else {
        System.out.println("Unknown operation! Returning to input area!");
        new FirstClass().calculatorvariables();
    }
    System.out.println("Do you want to do another calculation? Y/N");
    doAnotherCalculation = scanner.next().equalsIgnoreCase("Y");
    if (doAnotherCalculation == true) {
        new FirstClass().calculatorvariables();
    }
    else {
        new FirstClass().mainmenu();
    }

}

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

您应该在try / catch位之外声明变量,如下所示:

double y;

try {
  y = scanner.nextDouble();
catch (Exception e) {
  // Exception handling
}

答案 1 :(得分:0)

这应该有效

import java.util.Scanner;

public class TestSo{
    Scanner scanner = new Scanner(System.in);
    public static void main(String[] args){
        new TestSo().calculatorvariables();
    } 

    public void calculatorvariables() {
        System.out.println("Please enter the first number in your calculation.");

        double x = scanner.nextDouble();
        if(x != (double) x) {
            System.out.println("An error occurred! Did you input nothing or something other than a number? Returning to variable input screen!");
        } else
            System.out.println("Please enter the operator. Valid operators are: '+', '-', '*', '/'");
            String operation = scanner.next();
            System.out.println("Please enter the second number in your calculation.");
            double y = scanner.nextDouble();
            calculator(x, operation, y);
        }
    }

    public void calculator(double x, String operation, double y) {
        if(operation.equals("+")) {
            System.out.println(x + y);
        } else if(operation.equals("-")) {
            System.out.println(x - y);
        } else if(operation.equals("*")) {
            System.out.println(x * y);
        } else if(operation.equals("/")) {
            System.out.println(x / y);
        } else {
            System.out.println("Unknown operation! Returning to input area!");
            calculatorvariables();
        }
        System.out.println("Do you want to do another calculation? Y/N");
        boolean doAnotherCalculation = scanner.next().equalsIgnoreCase("Y");
        if (doAnotherCalculation == true) {
            calculatorvariables();
        } else {
            System.out.println("Done! Exiting");
        }

    }
}