Saturday 28 September 2013

Calculator system outprint result

Calculator system outprint result

could somone take a look at my calculator and tell me why its stopping
after asking me for numbers and what i wanna do if its add sub div or
multi but doenst print out the result or do you wanna continune yes or no
. Thanks in advance below is my code
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a,b,c;
int choice = 0;
a=b=c=0.0;
while(true) {
System.out.println("Enter two numbers");
a = Double.parseDouble(sc.nextLine());
b = Double.parseDouble(sc.nextLine());
System.out.println("Enter your choice");
System.out.println("1. Add");
System.out.println("2. Sub");
System.out.println("3. Mul");
System.out.println("4. Div");
choice = Integer.parseInt(sc.nextLine());
switch(choice) {
case 1 :
c = new Add().doMath(a,b);
break;
case 2 :
c = new Sub().doMath(a,b);
break;
case 3 :
c = new Div().doMath(a,b);
break;
case 4 :
c = new Mul().doMath(a,b);
break;
default:
break;
}
System.out.println(c + "this is the answere");
System.out.println("would you like to continue? (Y/N)");
if("N".equalsIgnoreCase(sc.nextLine())) { // careful with
the paranthesis
break;
}
}
}
}
interface MathOp {
public double doMath(double a, double b);
}
class Add implements MathOp{
public double doMath(double a, double b) {
return (a + b);
}
}
class Sub implements MathOp{
public double doMath(double a, double b) {
return (a - b);
}
}
class Div implements MathOp{
public double doMath(double a, double b){
return (a / b);
}
}
class Mul implements MathOp{
public double doMath(double a, double b){
return (a * b);
}
}

No comments:

Post a Comment