我尝试使用不同的选项(十六进制,二进制,八进制,十进制)制作桌面计算器,如果输入的值不是该模式,则会出现错误,例如&#34 ; BinaryInteger预计,用户输入909382"并再次提示。现在唯一出现的错误是"无效选项"由于我的捕获(例外e)。如何为无效输入编写4个异常/错误。
import java.util.*;
public class IntDriver
{
//declared fields
private static LongInteger num1;
private static LongInteger num2;
private static String opr;
private static int mode = 0;
// Operators that will be used
private final static String[] Operators = { "", "+", "-", "*", "/" };
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
boolean run = true;
while(run)
{
displayMenu();
while(true)
{
try
{
System.out.print("Option or value --> ");
String option = kb.nextLine();
switch(option.toLowerCase())
{
//modes
// decimal mode
case "decimal":
case "dcm":
case "dec":
mode = 0;
break;
// binary mode
case "binary":
case "bin":
mode = 1;
break;
//octal mode
case "octal":
case "oct":
mode = 2;
break;
//hex mode
case "hexadecimal":
case "hex":
mode = 3;
break;
//quit
case "q":
case "quit":
System.out.println("Thank you. Have a nice day.");
run = false;
break;
//Operators
//add
case "+":
opr = "+";
break;
//subtract
case "-":
opr = "-";
break;
//multiply
case "*":
opr = "*";
break;
//divide
case "/":
opr = "/";
break;
//equals
case "=":
operate();
break;
default:
LongInteger temp;
//with mode it's in:
switch (mode)
{
case 1:
temp = new BinaryInteger(option);
break;
case 2:
temp = new OctalInteger(option);
break;
case 3:
temp = new HexInteger(option);
break;
default:
temp = new DecInteger(option);
break;
}
// if no num1 = LongInteger temp.
if (num1 == null)
{
num1 = temp;
}
else
{
if (opr == null)
{
throw new UnsupportedOperationException();
}
num2 = temp;
}
break;
}
break;
}
catch (UnsupportedOperationException e)
{
System.out.println("Invalid option; operator not specified.\n");
}
// Invalid option entered.
catch (Exception e)
{
System.out.println("Invalid option.\n");
}
}
}
}
//Menu Display
private static void displayMenu()
{
System.out.println();
switch (mode)
{
case 1:
System.out.println("Binary Mode");
break;
case 2:
System.out.println("Octal Mode");
break;
case 3:
System.out.println("Hexadecimal Mode");
break;
default:
System.out.println("Decimal Mode");
break;
}
if (num1 != null)
{
System.out.print(num1 + "\t");
}
if (opr != null)
{
System.out.print(opr + "\t");
}
if (num2 != null)
{
System.out.print(num2 + "\n");
}
System.out.println("\n\n\tModes:\t\t Operators:");
System.out.println("\tBin - Binary\t\t+");
System.out.println("\tOct - Octal\t\t-");
System.out.println("\tDcm - Decimal\t\t*");
System.out.println("\tHex - Hexadecimal\t/");
System.out.println("\tQ - Quit\t\t=\n");
}
private static void operate() throws Exception
{
if (num1 == null || num2 == null || opr == null)
{
throw new Exception("Not enough numbers.");
}
switch (opr)
{
case "+":
num1 = num1.calcValue(opr, num2, mode);
break;
case "-":
num1 = num1.calcValue(opr, num2, mode);
break;
case "*":
num1 = num1.calcValue(opr, num2, mode);
break;
case "/":
num1 = num1.calcValue(opr, num2, mode);
break;
default:
throw new Exception("Invalid operator.");
}
num2 = null;
opr = "";
}
}
答案 0 :(得分:0)
你可以为每个案例延长java.lang.Exception次:
public class YourCustomException extends Exception {
//copy paste and modify whatever constructors are relevant in your case.
}
..然后当你的模式输入无效时继续抛出这4个异常。不过,这在你的情况下是相当低效的。我认为你的案例很简单,你可以简单地做一些事情:
public class DecInteger {
public DecInteger (String option)
{
if (isNotDecimal(option))
throw new Exception("Input option " + option + " is not a decimal");
}
}
..然后在你的catch区块你将需要做这样的事情:
catch(Exception ex) {
System.out.println(ex.getMessage);
}
从技术上讲,抛出Exception
类型的异常并不是一个好习惯,最好是创建更能描述实际异常的子类型,但是你的情况很简单,这并不是真的物质