I wanted to loop my calculator in a manner similar to Codecademy’s Area Calculator. I embraced most of my methods with a while loop’s curly brackets
public static void main(String[] args) {
Num firstNum = new Num();
Num secondNum = new Num();
System.out.println(getGreeting() + " I'm Alec the Calculator! I can perform the following operations: " +
"addition (+), subtraction (-), multiplication (*), and division (/). But I'm only a beginner! " +
"That's why I can only operate with integers of up to ten. Keep in mind, if you input more than two " +
"operands, I will execute only the first operation and ignore the rest. The good news is you can " +
"use both Arab (1, 2, 3...) and Roman numbers (I, II, III...)! ");
while (true) {
System.out.println("You can type in your math problem below:");
Scanner scanner = new Scanner(System.in);
firstNum.stringValue = scanner.next();
stringOperation = scanner.next();
secondNum.stringValue = scanner.next();
firstNum.checkType();
secondNum.checkType();
if (firstNum.type == 'd' || secondNum.type == 'd') {
firstNum.decimalRejection(firstNum, secondNum);
}
if (firstNum.type == 'i' || secondNum.type == 'i') {
integerHandling(firstNum, secondNum);
}
if (firstNum.type == 'r' || secondNum.type == 'r') {
romanHandling(firstNum, secondNum);
}
calculatingResult(firstNum, secondNum);
printingResult(firstNum, secondNum);
}
}
and added several break keywords inside the printingResult() method. It’s always called. There should not be any infinite loops (to the best of my knowledge)!
public static void printingResult(Num firstNum, Num secondNum) {
if (!(stringOperation.equals("/") && secondNum.doubleValue == 0)) {
if (!dontKnow) {
if (firstNum.type == 'r' && secondNum.type == 'r') {
if(result == 0){
System.out.println("Oops! It seems we got a zero – or \"nulla\", as Romans said. " +
"The Roman numeral system didn't have a zero, you see!");
break;
} else if(result < 0) {
System.out.println("Uh-oh... It looks like you subtracted more than you had in the first place! " +
"It's generally okay, but the problem is the Roman numeral system didn't have negative numbers");
break;
} else {
System.out.println("It's " + arabToRoman() + "!");
break;
}
} else {
if (result != (int) result) {
if (String.valueOf(result).equals(df.format(result))) {
System.out.println("It's " + result + "! If you don't like decimals, here's your integer " +
"answer: " + (int) result + ". I like decimals, though");
break;
} else {
System.out.println("It's approximately " + df.format(result) + "! " +
"Or, if we choose to leave out the decimal part, it's " + (int) result);
break;
}
} else {
System.out.println("It's " + (int) result + "!");
break;
}
}
} else {
break;
}
} else {
if (!dontKnow) {
System.out.println("Gosh! I tried to divide it by zero, as you requested, but my virtual head nearly " +
"exploded! I need to recover...");
quit();
} else {
System.out.println("Besides, you can't even divide by zero, I'm so told!");
break;
}
}
}
However, my IntelliJ IDEA refuses to run the program because it doesn’t see those breaks as part of any loop of switch. What should I do?