48 lines
1.5 KiB
Java
48 lines
1.5 KiB
Java
import java.util.Scanner;
|
|
|
|
public class Calculator {
|
|
|
|
public static void main(String args[]){
|
|
Scanner sc = new Scanner(System.in);
|
|
char ch = 'y',f;
|
|
do{
|
|
System.out.println("Enter the first number: ");
|
|
float a = sc.nextFloat();
|
|
|
|
System.out.println("Enter the operator (+,-,/,*): ");
|
|
char b = sc.next().charAt(0);
|
|
|
|
if((b != '+') && (b != '-') && (b != '/') && (b != '*') && (b != 'x')){
|
|
System.out.println("Invalid operator.");
|
|
}
|
|
|
|
System.out.println("Enter the second number: ");
|
|
float c = sc.nextFloat();
|
|
|
|
switch(b){
|
|
case '+':
|
|
float d = a+c;
|
|
System.out.println(+a+" + "+c+" = "+d);
|
|
break;
|
|
case '-':
|
|
float e = a-c;
|
|
System.out.println(+a+" - "+c+" = "+e);
|
|
break;
|
|
case '/':
|
|
float g = a / c;
|
|
System.out.println(+a+" / "+c+" = "+g);
|
|
break;
|
|
case 'x':
|
|
case '*':
|
|
float h = a*c;
|
|
System.out.println(+a+" * "+c+" = "+h);
|
|
break;
|
|
}
|
|
|
|
System.out.println("Do you want to perform another calculation (y/n)?");
|
|
f = sc.next().charAt(0);
|
|
} while(f == ch);
|
|
|
|
}
|
|
}
|