Friday, August 3, 2007

code: Number Guessing

***Simple GUI Based I/O***

I created this code to show how some basic Java GUI (graphical user interface) components work and how to use them in a program. GUI programs can get complicated and hard to code very quickly because there are so many parts to GUI programming in Java... This program will only use some simple GUI-based input/output with JOptionPane. In future, i will probably post code that goes a little bit more in-depth when it comes to Java GUI.

/**
* Author: http://javacodee.blogspot.com/
* Date: Aug 3, 2007
* Time: 11:08:31 PM
*/
import javax.swing.*;
import java.util.Random;

public class GuessNum
{
public static void main(String args[])
{
String guess =null;
int num =0;
int randomNumber =0;
int playAgain =0;
int counter =0;
Random generator = new Random();

do
{
randomNumber =generator.nextInt(100);

guess = JOptionPane.showInputDialog("I'm thinking of a number between 1 and 100...\nwhat is your guess?");
num =Integer.parseInt(guess);

/*
Since JOptionPane returns a String you have to use Integer.parseInt(guess) to convert the string to a new int value.
The 'Integer' class wraps a value of the primitive type 'int' in an object. If you need to assign the string to a double for example you would use Double.parseDouble(guess), another one you could use is Float.parseFloat(guess) if you need a float value. For all of the different methods and constructors please review the java documentation and look under java.lang.Object.
*/

while(num !=randomNumber)
{
if(num >randomNumber)
{
JOptionPane.showMessageDialog(null, "Nope, that's not it! Your number is too high\nTry again...",
"Guess Incorrect!", JOptionPane.INFORMATION_MESSAGE);

/*
The first string
"Nope, that's not it! Your number is too high\nTry again..." represents the text that will show up in the message dialog window and the second string "Guess Incorrect!" represents the text that will show up as the windows title located in the top part of the window.
*/

}
else
{
JOptionPane.showMessageDialog(null, "Nope, that's not it! Your number is too low\nTry again...",
"Guess Incorrect!", JOptionPane.INFORMATION_MESSAGE);
}

guess =JOptionPane.showInputDialog("I'm thinking of a number between 1 and 100...\nwhat is your guess?");
num =Integer.parseInt(guess);
counter++;

/*
Variable 'counter' will keep tract of the number of tries that took you to get the number guessed right
*/

}

playAgain =JOptionPane.showConfirmDialog(null, "After "+counter+" tries you got it!\nDo you want to play again?",
"Play again?", JOptionPane.YES_NO_OPTION);
}
while(playAgain != JOptionPane.NO_OPTION);

JOptionPane.showMessageDialog(null, "Thank you for playing!", "Goodbye", JOptionPane.INFORMATION_MESSAGE);
}
}

2 comments:

Unknown said...

hai am hiba,

will u please post me java program on "Examination results according to the classes"

hibafathima19@yahoo.com

Unknown said...

"My Bitcoin ATM is strongest and very easy to use when you get chance visit my site"