I'm writing a simple calculator in java where you can perform 4 basic operations with only 2 operands. What i need is that inside the if statement I want to perform a loop where a user will continue doing the same operation when ask to continue and if it quits it will be ask again to continue by selecting another operations. My problem is that it is not performing a loop inside the if statements instead it performs a loop where a user is ask to continue by selecting another operations.
import java.util.Scanner;
public class calculator
{
public static void main(String [] args)
{
Scanner input = new Scanner (System.in);
double x, y;
double operation;
String getChar;
String chooseOperation;
do
{
System.out.println("Choose your operation:");
System.out.println("A. Addition");
System.out.println("B. Subraction");
System.out.println("C. Multiplication");
System.out.println("D. Division");
chooseOperation = input.nextLine();
if ((chooseOperation.equals("A")) | (chooseOperation.equals("a")))
{
do
{
System.out.println("You Choose Addition\n\n\n");
System.out.println("Input the first number:\n");
x = input.nextDouble();
System.out.println("Input the second number:\n");
y = input.nextDouble();
operation = x+y;
System.out.println("The sum is :"+operation);
System.out.println("Continue?");
System.out.println("Y/N");
getChar=input.nextLine();
}while(getChar.equals("Y"));
}
else if ((chooseOperation.equals("B")) | (chooseOperation.equals("b")))
{
do
{
System.out.println("You Choose Subtraction\n\n\n");
System.out.println("Input the first number:\n");
x = input.nextDouble();
System.out.println("Input the second number:\n");
y = input.nextDouble();
operation = x-y;
System.out.println("The difference is :"+operation);
System.out.println("Continue?");
System.out.println("Y/N");
getChar=input.nextLine();
}while(getChar.equals("Y"));
}
else if ((chooseOperation.equals("C")) | (chooseOperation.equals("c")))
{
do
{
System.out.println("You Choose Multiplication\n\n\n");
System.out.println("Input the first number:\n");
x = input.nextDouble();
System.out.println("Input the second number:\n");
y = input.nextDouble();
operation = x*y;
System.out.println("The product is :"+operation);
System.out.println("Continue?");
System.out.println("Y/N");
getChar=input.nextLine();
}while(getChar.equals("Y"));
}
else if ((chooseOperation.equals("D")) | (chooseOperation.equals("d")))
{
do
{
System.out.println("You Choose Division\n\n\n");
System.out.println("Input the first number:\n");
x = input.nextDouble();
System.out.println("Input the second number:\n");
y = input.nextDouble();
operation = x/y;
System.out.println("The quotient is :"+operation);
System.out.println("Continue?");
System.out.println("Y/N");
getChar=input.nextLine();
}while(getChar.equals("Y"));
}
else
{
System.out.println("Invalid Selection!\n\n\n");
}
System.out.println("Do you want to continue?\n\nY/N");
getChar=input.nextLine();
}while((getChar.equals("Y"))|(getChar.equals("y")));
}
}
A , 6, 7and you see the problem. There is something wrong withinput.nextLine(). It gets skipped...