+ 1

this is my first attempt in creating a java program with a purpose. it is a primitive calculator. I need your guidance to improv

https://code.sololearn.com/cw1dp63T3k5L/?ref=app

2nd Dec 2017, 6:29 PM
Shakir Saeed Mohamed Saeed
2 ответов
+ 25
//here are some improvements ☺ //u can use 1 object of scanner class , to take multiple inputs import java.util.Scanner; public class Calc { public static void main(String[] args) { Addition a = new Addition(); Subtraction s = new Subtraction(); Multiplication multi = new Multiplication(); Division d = new Division(); Scanner m = new Scanner(System.in); double x,y,z=0; char c; while(true) { System.out.println("Enter 2 numbers:"); x = m.nextDouble(); y = m.nextDouble(); System.out.println("Enter operator:"); c = m.next().charAt(0); if(c == '+') z = a.add(x,y); else if(c == '-') z = s.sub(x,y); else if(c == '*'||c=='×') z = multi.mul(x,y); else if(c == '/'||c=='÷') z = d.div(x,y); else{ System.out.println("Your choice is not is available"); System.exit(0);} System.out.println(x+" "+c+" "+y+" = "+ z); } } } public class Addition { double add(double x,double y) { return x+y; } } public class Subtraction { double sub(double x,double y) { return x-y; } } public class Multiplication { double mul(double x,double y) { return x*y; } } public class Division { double div(double x,double y) { return x/y; } }
2nd Dec 2017, 6:45 PM
Gaurav Agrawal
Gaurav Agrawal - avatar