To summarize, I am attempting to clean my code up. The program is prompting a user to enter (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit - each letter is associated with an if statement. "q" will be the sentinel value exiting the program. User will need to enter two numbers which would then be executed by one of the above operations. My problem is that I cannot seem to solve why "Enter first number" is being prompted alongside "Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?". I am probably over looking the simplest thing. I hope my explanation was clear enough. Also, any suggestions on improving code readability would be greatly appreciated.
The goal was to ask the user how they would like to execute their two numbers. Once selected, they would then enter their two numbers - immediately followed by the sum. Once the sum is presented, they are asked again which operation to choice.
import java.util.Scanner;
public class ScratchCode {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String operationLetter;
int firstNumber;
int secondNumber;
System.out.println("Hello! Welcome to the world's worst calculator.\n");
do {
System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
operationLetter = input.nextLine();
System.out.println("Enter first number.");
firstNumber = input.nextInt();
System.out.println("Enter second number.");
secondNumber = input.nextInt();
if (operationLetter.equalsIgnoreCase("a")) {
int sum = firstNumber + secondNumber;
System.out.println("Great. " + firstNumber + " + " + secondNumber + " = " + sum);
}
else if (operationLetter.equalsIgnoreCase("s")) {
int sum = firstNumber - secondNumber;
System.out.println("Great. " + firstNumber + " - " + secondNumber + " = " + sum);
}
else if (operationLetter.equalsIgnoreCase("m")) {
int sum = firstNumber * secondNumber;
System.out.println("Great. " + firstNumber + " * " + secondNumber + " = " + sum);
}
else if (operationLetter.equalsIgnoreCase("d")) {
int sum = firstNumber / secondNumber;
System.out.println("Great. " + firstNumber + " / " + secondNumber + " = " + sum);
}
}
while(!operationLetter.equalsIgnoreCase("q"));
System.out.println("Goodbye !");
}
}