Java异常处理分配

时间:2014-02-09 02:09:07

标签: java exception-handling

所以我最近学习了Java的异常处理,我仍然试图习惯它,但我觉得我错过了一些东西。我正在为我的班做一个作业,编译器不喜欢我的代码。

我应该制作一个接收运算符然后是数字的计算器,如果给定的运算符(+, - ,*,/)不是上述四个之一,则抛出异常。

我使用了try-catch但是由于某种原因,try和catch没有互相识别,除非我把它们放在彼此旁边,这不是我想要的。

我的问题是:我有没有办法尝试互相识别对方,而不是彼此相邻,所以我可以在它们之间运行代码?或者这是不可能的,我做错了吗?

到目前为止,这是我的代码:

 import java.util.*;
 public class Calculator 
 {
        public static void main(String[] args)
    {
    String operatorInput;
    double numberInput;
    String userRedo = "Y";
    double result = 0.0;
    Scanner input = new Scanner(System.in);
    System.out.println("Type in an arithmetic operator of your choice and press enter.");
    System.out.println("Then, please type in a number that will undergo the operation.");

    while((userRedo.toUpperCase()).compareTo("Y")==0) 
    {
        try 
        {
            operatorInput = input.nextLine();
            if(operatorInput.compareTo("+")!=0||operatorInput.compareTo("-")!=0||
               operatorInput.compareTo("*")!=0||operatorInput.compareTo("/")!=0) 
            {
                throw new UnknownOperatorException("Unknown operator!");
            }   
        }

        numberInput = input.nextDouble();
        if(operatorInput.compareTo("+")==0) 
        {
            result += numberInput;
        } else if(operatorInput.compareTo("-")==0) 
        {
            result -= numberInput;
        } else if(operatorInput.compareTo("*")==0) 
        {
            result = result * numberInput;
        } else
        {
            result = result / numberInput;
        }

        System.out.println("\nresult "+operatorInput+" "+numberInput+"= ");
        System.out.println("Updated result: "+result);
        System.out.println("Again? (y/n)");
        userRedo = input.nextLine();


        catch(UnknownOperatorException e) 
        {
            System.out.println(e.getMessage());
        }   
    }
}

}

这是我做的异常课程:

    public class UnknownOperatorException extends Exception 
    {
public UnknownOperatorException() 
{
    super("Please select an actual operator and try again: ");
}

public UnknownOperatorException(String message)
{
    super(message);
}

}

4 个答案:

答案 0 :(得分:2)

他们必须彼此相邻。你可以做一些事情:

将注释行中的}向下移动,例如

    while((userRedo.toUpperCase()).compareTo("Y")==0) 
    {
        try 
        {
            operatorInput = input.nextLine();
            if(operatorInput.compareTo("+")!=0||operatorInput.compareTo("-")!=0||
               operatorInput.compareTo("*")!=0||operatorInput.compareTo("/")!=0) 
            {
                throw new UnknownOperatorException("Unknown operator!");
            }   

        }//this one here

把它移到这里

    System.out.println("\nresult "+operatorInput+" "+numberInput+"= ");
    System.out.println("Updated result: "+result);

}// put it here

然后拿这两行

System.out.println("Again? (y/n)");
userRedo = input.nextLine();

将它们移到捕获区下方:

        catch(UnknownOperatorException e) 
        {
            System.out.println(e.getMessage());
        }   
        System.out.println("Again? (y/n)");
        userRedo = input.nextLine();
    }
}

这会让你的while循环仍然有点功能,让你的try / catch工作。您可能需要稍微调整一下以使其正常工作

答案 1 :(得分:0)

为了让try / catch工作,正如Ghost在评论中所说,你需要将它们放在一起。

答案 2 :(得分:0)

上面的代码有一些问题......

也许这样可行。

public static void main(String[] args) {
    String operatorInput=null;
    double numberInput;
    String userRedo = "Y";
    double result = 0.0;
    Scanner input = new Scanner(System.in);
    System.out.println("Type in an arithmetic operator of your choice and press enter.");
    System.out.println("Then, please type in a number that will undergo the operation.");

    while ((userRedo.toUpperCase()).compareTo("Y") == 0) {
        try {
            operatorInput = input.nextLine().trim();
            if (operatorInput.compareTo("+") != 0
                    && operatorInput.compareTo("-") != 0
                    && operatorInput.compareTo("*") != 0
                    && operatorInput.compareTo("/") != 0) {
                throw new UnknownOperatorException("Unknown operator!");
            }

        } catch (UnknownOperatorException e) {
            System.out.println(e.getMessage());
            continue;
        }

        numberInput = Double.parseDouble(input.nextLine());

        if (operatorInput.compareTo("+") == 0) {
            result += numberInput;
        } else if (operatorInput.compareTo("-") == 0) {
            result -= numberInput;
        } else if (operatorInput.compareTo("*") == 0) {
            result = result * numberInput;
        } else {
            result = result / numberInput;
        }

        System.out.println("\nresult " + operatorInput + " " + numberInput + "= ");
        System.out.println("Updated result: " + result);
        System.out.print("Again? (y/n) : ");

        userRedo = input.nextLine();
    }

    input.close();
}

public static void main(String[] args) { String operatorInput=null; double numberInput; String userRedo = "Y"; double result = 0.0; Scanner input = new Scanner(System.in); System.out.println("Type in an arithmetic operator of your choice and press enter."); System.out.println("Then, please type in a number that will undergo the operation."); while ((userRedo.toUpperCase()).compareTo("Y") == 0) { try { operatorInput = input.nextLine().trim(); if (operatorInput.compareTo("+") != 0 && operatorInput.compareTo("-") != 0 && operatorInput.compareTo("*") != 0 && operatorInput.compareTo("/") != 0) { throw new UnknownOperatorException("Unknown operator!"); } } catch (UnknownOperatorException e) { System.out.println(e.getMessage()); continue; } numberInput = Double.parseDouble(input.nextLine()); if (operatorInput.compareTo("+") == 0) { result += numberInput; } else if (operatorInput.compareTo("-") == 0) { result -= numberInput; } else if (operatorInput.compareTo("*") == 0) { result = result * numberInput; } else { result = result / numberInput; } System.out.println("\nresult " + operatorInput + " " + numberInput + "= "); System.out.println("Updated result: " + result); System.out.print("Again? (y/n) : "); userRedo = input.nextLine(); } input.close(); }

答案 3 :(得分:0)

public class Building implements CarbonFootprint {
    
    //implement 4a
    void getCarbonFootprint(){
    System.out.println("Kim Byers, CSIS 505, Exception Handling, Assignment1");
    
    //Building display
    double Bill;
    }
    
    //class variables
    double monthlyElectricBill;
    double monthlyGasBill;
        
    //constructor
    public void Building(double monthlyElectricBill, double monthlyGasBill) {
        monthlyElectricBill = ([monthyly Electric Billmonth;
        monthlyGasBill = monthlyGasBill;
            
    //Constructor
        
    //set method statements
}