import java.io.*; // This line allows us to use commands to read from the command line public class Operator { // Name of the file public static void main(String[] args) throws IOException { // Main program start BufferedReader stdIn = new BufferedReader( // These lines allow us to read from the command line new InputStreamReader( System.in ) ); int x; // These lines tell java what variables we need to use int y; float result; String operator; String yesOrNo; do{ // This loop will always execute as long as the user does not enter "n" at the end System.out.print( "Enter the first number: " ); x = Integer.parseInt( stdIn.readLine() ); // Gets a number from the console and turns it // into an integer System.out.print( "Enter the second number: " ); y = Integer.parseInt( stdIn.readLine() ); System.out.print( "What operation do you want to do? ( + , - , * , / ) " ); operator = stdIn.readLine(); // User enters what operation they want to use if( operator.compareTo( "+" ) == 0 ) // If user wants to add, then add x and y result = x + y; else if( operator.compareTo( "-" ) == 0 ) // If user wants to subtract, then subtract x and y result = x - y; else if( operator.compareTo( "*" ) == 0 ) // If user wants to multiply, then multiply x and y result = x * y; else if( operator.compareTo( "/" ) == 0 ) // If user wants to divide, then divide x and y result = ( float )x / ( float )y; // This one tells java not to round answers and instead use floating point else{ // If no valid operator was entered, the following message is displayed System.out.println( "Not a valid operator, try again" ); break; // Breaks the loop and ends the program } System.out.println( x + " " + operator + " " + y + " = " + result ); // Tells java to output the result System.out.println(); // Blank line System.out.print( "Would you like to add two more numbers? (y/n) " ); yesOrNo = stdIn.readLine(); // Get the answer from console if( yesOrNo.compareToIgnoreCase( "n" ) == 0 ) // If the answer was "n" then we exit the program System.exit( 0 ); } while( true ); // Goes back to the beginning of the "do" loop if the person did not enter "n" to stop the program } }