Reputation: 113
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")));
}
}
Upvotes: 1
Views: 5278
Reputation: 2104
Its a long time ago I programmed java. But I think its because you're using only one |
. I'm not sure but I thought java needs two |
. So your first if will be:
if (chooseOperation.equals("A") || chooseOperation.equals("a"))
{
}
Upvotes: 0
Reputation: 2223
Read the entire line every time then parse into double
:
//Addition
do
{
System.out.println("You Choose Addition\n\n\n");
System.out.println("Input the first number:\n");
x = Double.parseDouble(input.nextLine());//read the whole line, then parse into double
System.out.println("Input the second number:\n");
y = Double.parseDouble(input.nextLine());//read the whole line, then parse into double
operation = x + y;
System.out.println("The sum is :" + operation);
System.out.println("Continue?");
System.out.println("Y/N");
getChar = input.nextLine();//when not reading the whole line, getChar is empty
}
while (getChar.equals("Y"));
Otherwise, getChar
seem to contain the next line character created when user input the numbers (type the number then press Enter).
Upvotes: 1