사용자가 다음 중 하나 이상을 입력하면 UnknownOperatorException이 throw되어야합니다. + - PR/
그러나 제대로 작동하지 않는 것 같습니다. 다음은 샘플 출력입니다.UnkownOperatorException이 발생하지 않는 이유는 무엇입니까?
Enter an operator and a number:
+5
Enter an operator and a number:
g9
5.0
메인 클래스는 특별히 메시지를 출력 적이지만, 예외가 발생되고있어서 않는다 .. 그래서 예외가 잡히면 메시지를 인쇄하지 않겠습니까?
public class Main{
public static void main (String[] args) throws Exception {
Calculator a = new Calculator(0);
a.setNumber(a.aResult(a));
a.setNumber(a.aResult(a));
String theString = String.valueOf(a.getNumber());
System.out.println(theString);
}
}
이 예외
public void whatOperator() throws Exception
{
String operatorString = null;
operatorString = enterNumber();
// shouldn't this test the string and throw the exception if needed?
throwOperatorException(operatorString);
if(operatorString.substring(1).equals(""))
{
switch(operatorString){
case "R":
result = RESET;
break;
case "P":
System.out.println("Goodbye");
System.exit(0);
}
}
else
theNumber = Double.parseDouble(operatorString.substring(1));
char theOperator = operatorString.charAt(0);
this.operator = theOperator;
operatorString ="";
operatorString += theOperator;
switch(operatorString){
case "*":
result = getNumber() * theNumber;
break;
case "/":
result = getNumber()/theNumber;
break;
case "+":
result = getNumber() + theNumber;
break;
case "-":
result = getNumber() - theNumber;
break;
}
}
에게 마지막으로 예외 방법 자체를 throw하는 메서드를 호출하는 방법입니다 :
여기 내 주요 클래스입니다. 어떻게 든 그것은 던져지지 않고있다. 내 생각 엔 그 안에 중첩 된 if 문과 관련이 있다고 생각하지만이 문제를 해결하는 방법은 확실하지 않습니다.
public void throwOperatorException(String entry) throws Exception
{
char oneOperator;
for(int i = 0; i < ALL_OPERATORS.length();i++)
{
oneOperator = ALL_OPERATORS.charAt(i);
if(entry.charAt(0) != oneOperator && i == ALL_OPERATORS.length())
{
try{
throw new UnkownOperatorException(entry);
}catch(UnkownOperatorException e){
System.out.println(e.getMessage());
}
}
}
}
이 사실이 결코
i == ALL_OPERATORS.length()
이후 ALL_OPERATORS
private final String ALL_OPERATORS = "+-*/RP";
이 의미가 있습니다. 그래서 나는 길이에서 1을 뺄 필요가 있습니다 .... – mark1092
@ mark1092는 효과가 있습니다.하지만 시도해 보지 않으면 보장되지 않습니다. – nullpointer