Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试捕获导致我的公共void出错的错误_Java_Exception Handling_Try Catch - Fatal编程技术网

Java 尝试捕获导致我的公共void出错的错误

Java 尝试捕获导致我的公共void出错的错误,java,exception-handling,try-catch,Java,Exception Handling,Try Catch,在你提问之前,我已经看了其他问题,它们似乎只是针对稍微不同的事情。 我是java新手,目前正在学习,所以我制作了这个自恋的程序,我将把它制作成一个多工具的东西。那部分其实并不重要。 基本上,我在做一个文本输入计算器,你可以输入数字、运算等等,但它相当简单。然而,在输入区域中,我正在尝试捕捉,以防用户键入非数字的内容。但是,这使得x和y(用户输入)的变量在初始化单独的公共无效计算器(变量)时未初始化且不可读。这是我的代码(我只包括了计算器部分,其余部分都是无关的,工作正常,而且我知道x和y的处理方

在你提问之前,我已经看了其他问题,它们似乎只是针对稍微不同的事情。 我是java新手,目前正在学习,所以我制作了这个自恋的程序,我将把它制作成一个多工具的东西。那部分其实并不重要。 基本上,我在做一个文本输入计算器,你可以输入数字、运算等等,但它相当简单。然而,在输入区域中,我正在尝试捕捉,以防用户键入非数字的内容。但是,这使得x和y(用户输入)的变量在初始化单独的公共无效计算器(变量)时未初始化且不可读。这是我的代码(我只包括了计算器部分,其余部分都是无关的,工作正常,而且我知道x和y的处理方式不同,我正在测试它们)


任何帮助都将不胜感激

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

double y;

try {
  y = scanner.nextDouble();
catch (Exception e) {
  // Exception handling
}
这应该行得通

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

    }
}

这就是“声明”变量。这是解决方案的一半。实际上,在
try catch
之外初始化它(即分配一个值)会有更大的帮助。哎呀,我把它修好了。
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");
        }

    }
}