Ask a Teacher



in java (blue j)software how can i create a calculator which can +,-,*,/.

import java.io.*; 
import java.util.*; 
public class Calculator 
public static void main(String args[]) 
System.out.println("Make your arithmetic selection from the choices below:\n"); 
System.out.println(" 1. Addition"); 
System.out.println(" 2. Subtraction"); 
System.out.println(" 3. Multiplication"); 
System.out.println(" 4. Division\n"); 

System.out.print(" Your choice? "); 


Scanner kbReader = new Scanner(System.in); 
int choice = kbReader.nextInt(); 


if((choice<=4) && (choice>0)) 

System.out.print("\nEnter first operand. "); 
double op1 = kbReader.nextDouble(); 
System.out.print("\nEnter second operand."); 
double op2 = kbReader.nextDouble(); 

System.out.println(""); 

switch (choice) 
case 1: //addition 
System.out.println(op1 + " plus " + op2 + " = " + (op1 + op2) ); 
break; 
case 2: //subtraction 
System.out.println(op1 + " minus " + op2 + " = " + (op1 - op2) ); 
break; 
case 3: //multiplication 
System.out.println(op1 + " times " + op2 + " = " + (op1 * op2) ); 
break; 
case 4: //division 
System.out.println(op1 + " divided by " + op2 + " = " + (op1 / op2) ); 

else 

System.out.println("Please enter a 1, 2, 3, or 4."); 
}


comments powered by Disqus