Calculator

Run Settings
LanguageJava
Language Version
Run Command
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int ans = 0; while (true) { System.out.print("Enter ( * + - % / ) or X to exit : "); char op = in.next().trim().charAt(0); if(op == '*' || op == '+' || op == '-' || op == '/' || op == '%'){ System.out.print("Enter two numbers : "); int a = in.nextInt(); int b = in.nextInt(); if(op == '*'){ ans = a * b; } if(op == '+'){ ans = a + b; } if(op == '-'){ ans = a - b; } if(op == '%'){ if(b != 0){ ans = a % b; } } if(op == '/'){ if(b != 0){ ans = a / b; } } } else if(op == 'x' || op == 'X'){ System.out.print("Exited."); break; }else{ System.out.print("Invalid Char"); break; } System.out.println("Answer = " + ans + " "); } } }
Editor Settings
Theme
Key bindings
Full width
Lines