# S.O.S. Mathematics CyberBoard

Your Resource for mathematics help on the web!
 It is currently Thu, 23 May 2013 22:38:40 UTC

 All times are UTC [ DST ]

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Author Message
 Member

Joined: Wed, 8 Jun 2005 02:50:42 UTC
Posts: 49
This program is to write a Java main class to play a number guessing game. The user thinks of a number between 1 and 128 and the computer guesses the number by asking several yes/no questions.
The program's main class should:

- Use JOptionPane to tell the user to think of a numer between low = 1 and high = 128
- Start a while loop
- Guess at the number by calculating the number half way between low and high, (low+high)/2
- Use JOptionPane to ask the user if your guess is correct; yes or no
- If the answer is yes the break from the loop
- If the answer is no then ask the user if your guess is bigger than the nunber, yes or no
- End the while loop
- Display the correct guess and the number of guesses

--------------------------------------------------------------------------------
import javax.swing.JOptionPane;

public class game {

public game() {
int low = 1, high = 128, guess = 0;
String check = "", check2;
boolean check3 = true;
JOptionPane.showMessageDialog(null,"Think of a number between 1 and 128");

while(true){
guess = (low + high)/2;
check = JOptionPane.showInputDialog("Is your number " + guess +"? (y/n)");
if(check.equalsIgnoreCase("Y")){
JOptionPane.showMessageDialog(null, "The number you guessed is "+ guess);
check3 = false; break;
}
else if (check.equalsIgnoreCase("N")){
check3 = true;
check2 = JOptionPane.showInputDialog("Is the number greater than or less than "+ guess+"? (< >)");
if(check2.equals("<")) //less than
high = guess;
else if(check2.equals(">")) // greater than
low = guess;
}
else
check = JOptionPane.showInputDialog("Please specify (Y or N):");
}
}

public static void main(String[] args) {
new game();
}

Top

 Post subject: Posted: Wed, 8 Jun 2005 22:06:36 UTC
 Member of the 'S.O.S. Math' Hall of Fame

Joined: Wed, 1 Oct 2003 04:45:43 UTC
Posts: 9633
Hi Sami,

Did you have any questions? For the most part, your code works fine. I have some comments, though:

(1) The program doesn't print out the number of guesses after the game is over. It should do this (according to the specs you provided).

(2) Choosing 'cancel' any time during the game causes a NullPointerException to be thrown. You may or may not want to handle this.

(3) If I guess the number 128, then the game never ends (the highest number the computer ever guesses is 127).

(4) It is the convention that every Java class begins with a capital letter. I would change the name of the class (and consequently that of the constructor) to Game instead of game.

Otherwise, everything looks good to me. Nice work so far.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 2 posts ]

 All times are UTC [ DST ]

#### Who is online

Users browsing this forum: No registered users

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ High School and College Mathematics    Algebra    Geometry and Trigonometry    Calculus    Matrix Algebra    Differential Equations    Probability and Statistics    Proposed Problems Applications    Physics, Chemistry, Engineering, etc.    Computer Science    Math for Business and Economics Advanced Mathematics    Foundations    Algebra and Number Theory    Analysis and Topology    Applied Mathematics    Other Topics in Advanced Mathematics Other Topics    Administrator Announcements    Comments and Suggestions for S.O.S. Math    Posting Math Formulas with LaTeX    Miscellaneous