import java.io.*; public class PlayTime // This program takes a pair of integers and returns their sum or product etc. { public static void main (String [] args) throws Exception { InputStreamReader ca = new InputStreamReader (System.in); BufferedReader va = new BufferedReader (ca); // Thus we've connected the input stuff PrintWriter bon = new PrintWriter (System.out, true); // and here the output is connected int x=0, y=0, z=0; // x and y are for the input integers, and z for the answer int tries; // this is to allow the user a finite number of silly inputs int MAX_TRIES=3; // this sets the max number of tries char repond1='q', repond2='q'; // repond1 is for the arithmetic operator, repond2 is for repeat options String op_string; boolean success; String oops = "Sorry, I didn't understand what you typed"; // various useful strings String regoof = ". Try again (you have "; String ask = "Enter an integer "; String ask_op = "Enter the symbol (+,-,*,/,or %) for your desired calculation."; do // this do-while loop performs the calculation, and allows for the repeating of the program. { tries = 0; // they haven't tried and messed up yet!!! This is reset for each new calculation. success = false; // for new calculations they must (again) succeed in acquiring the data.44 while (tries <= MAX_TRIES && !success) { success = true; // thus success is only false if repond1 fails to be +, -, *, /, or % bon.println(ask + "x."); // we ought to check that what they type is an integer! x = Integer.parseInt (va.readLine()); // this acquires the first integer bon.println(ask + "y."); // checking for ints is best done by catching exceptions y = Integer.parseInt (va.readLine()); // this acquires the second integer bon.println(ask_op); op_string = va.readLine(); if (op_string.length() >= 1) repond1 = op_string.charAt(0); // this acquires the desired operator else success = false; // so if they don't type anything, we don't try to get the 1st character! if (success) // so don't bother getting more stuff if they've already goofed! { switch (repond1) // do the actual calculations!!! { case '+': z = x + y; break; // what would happen if we didn't have these 'breaks'? case '-': z = x - y; break; case '*': z = x * y; break; case '/': z = x / y; break; case '%': z = x % y; break; default : success = false; } } // end of answer-calculating 'if' section if (!success) // note that this catches goofy 'operators' as well as 'empty' ones { bon.print(oops); tries++; // thus set to 1 for the first bad attempt -- incremented each mess up if (tries == 1) bon.println(regoof +MAX_TRIES + " tries left)."); else if (tries < MAX_TRIES) bon.println(" again" +regoof +(MAX_TRIES - tries + 1) + " tries left)."); else if (tries == MAX_TRIES) bon.println(" again" +regoof + "one try left)."); else bon.println(" again. Too many errors -- a bientot!"); } // end of complaining-to-user 'if' section } // end of while loop -- exit loop if too many tries (>3) or success == true if (success) // so only show an answer if inputs are successful!! { bon.print("x was " + x + " and y was " + y); bon.println(" so z = x " + repond1 + " y is " + z + "."); bon.println("run again?"); // note they're only invited to run again if successful! repond2 = (va.readLine()).charAt(0); } // end of answer-display code, governed by 'if' } while (repond2 == 'y' && success); // end of do-while loop for allowing re-running the program. bon.println("Au revoir!"); ca.close(); } // end of main method } //end of class PlayTime