/** * Play the game of 24, where you are given 4 numbers and you must apply the * basic mathematical operators (+, -, *, /) to the four numbers (using each * number exactly once) to yield the value 24. * * Class: CS 102, Spring 2009 * Lab: Billie Joe Armstrong, Wed. 6:00 AM * System: BlueJ 2.1.1, jsdk 1.5, Windows XP * * @author Dale Reed * @version January 13, 2009 * * Running the program looks like (where what the user thinks of is shown in parenthesis, but doesn't apear on screen: */ // Import libraries needed by the program import java.util.Scanner; // used for console input // Declare the class public class TwentyFourB { // Fields that can be accessed anywhere in the class go here Scanner keyboard = new Scanner( System.in); // used to read user input //---------------------------------------------------------------------------------------- // main() - startup main loop. It is necessary to create an instance of this class // and then call a method from that instance, otherwise there are all kinds // of error messages having to do with non-static objects (e.g. keyboard) // being called from a static context (e.g. main). By creating the instance // and *then* using keyboard, it is not longer being called from a static // context. Don't worry about understanding all this right now. public static void main(String[] args) { // create an instance of this class TwentyFourB theTwentyFourBInstance = new TwentyFourB(); // call a non-static method to do everything else theTwentyFourBInstance.mainLoop(); } //---------------------------------------------------------------------------------------- // mainLoop() - display identifying information and run main loop. // In your program, you will only add code inside this method. You don't need to // change anything above except for the comments at the top of the program. // void mainLoop() { // Display identifying information System.out.println( "Author: Dale Reed \n" + "Program: #1, TwentyFour \n" + "TA: Englebert Humberdink, T 4-5 \n"+ "Jan 13, 2009"); System.out.println(); //Display game instructions and pause display System.out.println( "Welcome to the game of TwentyFour. Choose one of the sets of 4 numbers below .\n" + "You then need to use each of those four numbers exactly once, combining them somehow \n" + "with the basic mathematical operators (+,-,*,/) to yield the value twenty-four. \n" + "Possible sets of 4 numbers are: \n" + " 8 5 7 5 9 8 \n" + " 5 1 1 1 8 8 9 4 2 2 6 4 \n" + " 8 6 2 3 5 2 \n" + " \n"); // declare variables String operator = ""; // stores user input int number = 0; int answer = 0; // Prompt for and read in the 4 numbers to be used System.out.print("Please enter the first number: "); answer = keyboard.nextInt(); int loopCounter = 1; // value counts from 1..3, representing the 3 pairs of numbers to be used. // execute the loop 3 times, since there are 3 operators to be applied while( loopCounter <4) { // find out which operator is to be used System.out.print( "Enter the operator to be used (+,-,*, or /): "); operator = keyboard.next(); // read user input System.out.print( "Enter the next number: "); number = keyboard.nextInt(); // read the next number // apply the operator to the two values: answer and nextNumber if( operator.equals("+") ) { answer = answer + number; } else if( operator.equals("-") ) { answer = answer - number; } else if( operator.equals("*") ) { answer = answer * number; } else if( operator.equals("/") ) { answer = answer / number; } // display the answer System.out.println("That gives: " + answer ); System.out.println(); // increment loop counter loopCounter = loopCounter + 1; }//end while( loopCounter... // Leave a blank line before the answer System.out.println(); // Now check the answer if( answer == 24) { // answer was correct System.out.println("Well done, genius! \n"); } else { // Answer was not correct System.out.println("Better luck next time. \n"); } System.out.println("Goodbye, thanks for playing. \n"); }//end mainLoop() }//end class MagicNumber