Reputation:
I'm trying to create simple calculator in java with user input for first number and second number and then with while loop calculate as user like but when I wrote for example a instead of sum two number it broke loop!! why?
package com.myproject.CalcEngine2;
import java.util.Scanner; // import the Scanner class
public class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter fisr number");
double val1 = myObj.nextDouble();
System.out.println("Enter fisr number");
double val2 = myObj.nextDouble();
double result = 0.0d;
String addition = "a";
System.out.println("Enter what you want to do");
String opCode = myObj.next();
while(opCode.equals("E")) {
if (opCode.equals(addition))
result = val1 + val2;
else if (opCode.equals("s"))
result = val1 - val2;
else if (opCode.equals("d"))
result = val1 / val2;
else if (opCode.equals("m"))
result = val1 * val2;
System.out.println(result);
System.out.println("Enter what you want to do"+"E for exit , a for addition , s for subtraction , d for Division , m for multiplication ");
opCode = myObj.next();
}
}
}
program shows for every char that I wrote it will be false but I can't understand why!?
Upvotes: 0
Views: 1939
Reputation:
Thanks all And here you are final code for who will search in future with remove case sensitivity!
package com.myprogram.CalcEngine;
import java.util.Scanner; // import the Scanner class
public class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter fisr number");
double val1 = myObj.nextDouble();
System.out.println("Enter fisr number");
double val2 = myObj.nextDouble();
double result = 0.0d;
String addition = "a";
System.out.println("Enter what you want to do");
String opCode = myObj.next().toLowerCase();
while(!opCode.equals("e")) {
if (opCode.equals(addition)){
result = val1 + val2;
System.out.println(result);}
else if (opCode.equals("s")){
result = val1 - val2;
System.out.println(result);}
else if (opCode.equals("d")){
result = val1 / val2;
System.out.println(result);}
else if (opCode.equals("m")){
result = val1 * val2;
System.out.println(result);}
else
System.out.println("Error please enter true character: ");
System.out.println("Enter what you want to do"+"E for exit , a for addition , s for subtraction , d for Division , m for multiplication ");
opCode = myObj.next();
}
System.out.println("Good bye! See you :) ");
}
}
with change
String opCode = myObj.next();
to
String opCode = myObj.next().toLowerCase();
in line 15 and change
opCode = myObj.next();
to
opCode = myObj.next().toLowerCase();
in line 32
All the input will be lowercase. All the best.
Upvotes: 0
Reputation: 47
Please check the condition in the while loop, it terminates if the entered input is not 'E'.
To make it work change as follows:
while(!opCode.equals("E")) {
// Operation code
}
BTW, to be more clear with the operations, you can print as below:
System.out.println("Enter what you want to do, 'a': for addition, 's': for subtraction, 'd': for division, 'm': for multiplication ");
Upvotes: 0
Reputation: 10
The condition you have entered ,
while(opCode.equals("E")){
}
is not correct for this scenario. It will not be executed the block inside while loop, until you input E
.
So, Then none of your if else conditions satisfy.
Adding,
while(!opCode.equals("E")){
}
for the code will help you. Thanks!
Upvotes: 0
Reputation: 4044
Perhaps because the while loop will terminate except for "E"?
while(opCode.equals("E")) {
only runs when it "E" but I think you mean
while(!opCode.equals("E")) {
Upvotes: 3