In school I had to make a calculator program. In the program, we ask the user if they want to add, subtract, multiply, or divide. At the end, we ask the user of they want to continue the program or no. I haven't put in the looping part yet, but my problem here is that after the "Would you like to continue" is displayed, the program just exits.
package calculator;
import java.util.Scanner;
public class calculator {
public static void main(String[] args) {
{
int o1; //first operand
int o2; //second operand
Scanner input = new Scanner (System.in);
System.out.println("Enter a choice:");
System.out.println("+ to add");
System.out.println("- to subtract");
System.out.println("* to multiply");
System.out.println("/ to divide");
System.out.println("X to exit");
String userChoice = input.nextLine();
if (userChoice.equals("X to exit")) {
System.exit(0);
}
System.out.println("Enter the first operand:");
o1 = input.nextInt();
System.out.println("Enter the second operand:");
o2 = input.nextInt();
if (userChoice.equals("+ to add")) {
System.out.println( (o1) + (o2) ); }
else if (userChoice.equals("- to subtract")) {
System.out.println( (o1) - (o2) ); }
else if (userChoice.equals("* to multiply")) {
System.out.println( (o1) * (o2) ); }
else if (userChoice.equals("/ to divide")) {
System.out.println( (o1) / (o2) ); }
System.out.println("Would you like to continue?");
System.out.println("Yes");
System.out.println("No");
String userPick = input.nextLine(); {
if (userPick.equals("Yes")) {
System.out.println("Ok."); }
else if (userPick.equals("No")) {
System.exit(0); }
}
}
}
// TODO Auto-generated method stub
}