/** * The Arithmetic2 class implements an application that * calculates +, -, *, / for pairs of integers which have * been requested from the keyboard, and decides which * operation to do based on an input character. */ import java.io.*; class Arithmetic2 { public static void main(String[] args) throws Exception { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader inComing = new BufferedReader(isr); PrintWriter outGoing = new PrintWriter(System.out, true); char op, query; int x, y, quittingTime = 0; // quittingTime is to boot someone off if they're being obstinately truculent double z = 0; String ask = "Please enter an "; boolean again = false, sillySausage = false; do { outGoing.println(ask + "integer."); x = Integer.parseInt(inComing.readLine()); outGoing.println(ask + "operator."); op = (inComing.readLine()).charAt(0); outGoing.println(ask + "integer."); y = Integer.parseInt(inComing.readLine()); do { sillySausage = false; switch(op) { case '+' : { z = x + y; break; } case '-' : { z = x - y; break; } case '*' : { z = x * y; break; } case '/' : { if (y != 0) z = (double) x / y; else z = x / y; // this will throw an exception!!!! break; } default : { outGoing.print("Awfully sorry, but the only operators I know "); outGoing.println("about are + , - , * and /"); outGoing.println(ask + "operator."); op = (inComing.readLine()).charAt(0); sillySausage = true; //complaining that the char isn't an operator!!!!!!!! quittingTime++ ; } } } while ( sillySausage && ( quittingTime < 3) ); if (quittingTime < 3) { outGoing.println("The value of " + x + op + y + " is " + z); outGoing.println("Would you like to do another calculation?"); query = (inComing.readLine()).charAt(0); if ( (query == 'y') || (query == 'Y') ) again = true; } else again = false; } while ( again ); if (quittingTime < 3 ) outGoing.println("Thanks for using \"Arithmetic\", please come again!!"); else outGoing.println("Thanks for visiting us. English classes are available!"); } } // end of class Arithmetic2