The Artima Developer Community
Sponsored Link

Java Answers Forum
please some one help me

1 reply on 1 page. Most recent reply: Feb 5, 2009 6:00 PM by r g

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
dhol shah

Posts: 1
Nickname: keyurdhol
Registered: Jan, 2009

please some one help me Posted: Jan 22, 2009 10:34 PM
Reply to this message Reply
Advertisement
i need help with creating this programming for a game call 24.
here is description.
please someone help me in creating the java source code for this.



Welcome to the game of TwentyFour. Choose one of the sets of 4 numbers below .
You then need to use each of those four numbers exactly once, combining them somehow
with the basic mathematical operators (+,-,*,/) to yield the value twenty-four.
Possible sets of 4 numbers are:
8,8,5,1


Please enter the first number: 8
Enter the operator to be used (+,-,*, or /): -
Enter the next number: 5
That gives: 3


Enter the operator to be used (+,-,*, or /): *
Enter the next number: 8
That gives: 24


Enter the operator to be used (+,-,*, or /): /
Enter the next number: 1
That gives: 24


Well done, genius!
Goodbye, thanks for playing.


r g

Posts: 2
Nickname: meka1
Registered: Feb, 2009

Re: please some one help me Posted: Feb 5, 2009 6:00 PM
Reply to this message Reply
/**
* 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

Flat View: This topic has 1 reply on 1 page
Topic: Need a little help here =) Previous Topic   Next Topic Topic: I need help with some code plz!!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use