GUESS NUMBER

GUESS NUMBER

View Answers

March 27, 2009 at 12:29 AM

hi friend,

This code will help you:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class GuessNumber extends JFrame implements ActionListener{

private JPanel panel = new JPanel(new BorderLayout());
private JPanel panelButtons = new JPanel(new FlowLayout());
private JPanel panelBottom = new JPanel(new BorderLayout());
private JTextField fieldBox = new JTextField(10);
private JButton button1 = new JButton("Try");
private JButton button2 = new JButton("Quit");
private JLabel comment = new JLabel("guess ..");
private int randomNumber;

public GuessNumber() {
super("Guess Number");
randomNumber = new Random().nextInt(100) + 1;
button1.addActionListener(this);
button2.addActionListener(this);
add(buildWindow());
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
private JComponent buildWindow() {
panelButtons.add(button1);
panelButtons.add(button2);
panelBottom.add(panelButtons, BorderLayout.NORTH);
panelBottom.add(comment, BorderLayout.SOUTH);
panel.add(new JLabel(" Guess a number from 1 to 100 "), BorderLayout.NORTH);
panel.add(fieldBox, BorderLayout.CENTER);
panel.add(panelBottom, BorderLayout.SOUTH);
return panel;
}

public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();

if (obj.equals(button1)){
comment.setText(" ");
compareResult();
} else if (obj.equals(button2)){
System.exit(0);
}
}
private void compareResult(){
int userInput=0;
int diff;
int Difference;
try {
userInput = Integer.parseInt(
fieldBox.getText().trim());
} catch (Exception ex){
comment.setText("Enter a VALID number!");
return;
}
if (userInput == randomNumber){
JOptionPane.showMessageDialog(null, "CONGRATULATIONS! You got it!!",
"Random Number: " + randomNumber, JOptionPane.INFORMATION_MESSAGE);
randomNumber = new Random().nextInt(100) + 1;
return;
}
if (userInput > randomNumber){
comment.setText( "Too High. Try a lower number." );
diff=userInput - randomNumber;
Difference=Math.abs(diff);
} else {
comment.setText( "Too Low. Try a higher number." );
diff=randomNumber - userInput;
Difference=Math.abs(diff);
}
if(Difference<=50){
comment.setText("Freezing");
}
if(Difference<=25){
comment.setText("Cold");
}
if(Difference<=15){
comment.setText("Cool");
}
if(Difference<=10){
comment.setText("Warm");
}
if(Difference<=5){
comment.setText("Hot");
}
if(Difference<5){
comment.setText("Boiling");
}
else {
}
}
private void setBackgroundColor(Color color){
panel.setBackground(color);
panelBottom.setBackground(color);
panelButtons.setBackground(color);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GuessNumber GuessNumber = new GuessNumber();
GuessNumber.setVisible(true);
}
});
}
}

Thanks
RoseIndia Team









Related Tutorials/Questions & Answers:
GUESS NUMBER - Java Beginners
GUESS NUMBER  Create a guessing game that picks a random number...() { super("Guess Number"); randomNumber = new Random().nextInt(100) + 1...); panelBottom.add(comment, BorderLayout.SOUTH); panel.add(new JLabel(" Guess a number from
ModuleNotFoundError: No module named 'guess-a-number'
ModuleNotFoundError: No module named 'guess-a-number'  Hi, My... named 'guess-a-number' How to remove the ModuleNotFoundError: No module named 'guess-a-number' error? Thanks   Hi, In your python
Advertisements
ModuleNotFoundError: No module named 'Guess_Number'
ModuleNotFoundError: No module named 'Guess_Number'  Hi, My Python... 'Guess_Number' How to remove the ModuleNotFoundError: No module named 'Guess_Number' error? Thanks   Hi, In your python
guess number - Java Interview Questions
guess number  i am doing a java program for user to guess number. i... .."); private int randomNumber; public guess2() { super("Guess Number..., BorderLayout.SOUTH); panel.add(new JLabel(" Guess a number from 1 to 500 "), BorderLayout.NORTH
Guess Number Games in Java
Guess Number Games in Java Here we are going to create guess number game where user have to guess the randomly generated number. For this, we have allowed the user to enter any number through the textfield and click the 'try' button
ModuleNotFoundError: No module named 'guess'
ModuleNotFoundError: No module named 'guess'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'guess' How to remove the ModuleNotFoundError: No module named 'guess'
ModuleNotFoundError: No module named 'guess-language'
ModuleNotFoundError: No module named 'guess-language'  Hi, My... named 'guess-language' How to remove the ModuleNotFoundError: No module named 'guess-language' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'guess-logo'
ModuleNotFoundError: No module named 'guess-logo'  Hi, My Python... 'guess-logo' How to remove the ModuleNotFoundError: No module named 'guess-logo' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'gender-guess'
ModuleNotFoundError: No module named 'gender-guess'  Hi, My Python... 'gender-guess' How to remove the ModuleNotFoundError: No module named 'gender-guess' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'guess-language'
ModuleNotFoundError: No module named 'guess-language'  Hi, My... named 'guess-language' How to remove the ModuleNotFoundError: No module named 'guess-language' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'guess-logo'
ModuleNotFoundError: No module named 'guess-logo'  Hi, My Python... 'guess-logo' How to remove the ModuleNotFoundError: No module named 'guess-logo' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'guess-the-movie'
ModuleNotFoundError: No module named 'guess-the-movie'  Hi, My... named 'guess-the-movie' How to remove the ModuleNotFoundError: No module named 'guess-the-movie' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'guess-indian-gender'
ModuleNotFoundError: No module named 'guess-indian-gender'  Hi, My... named 'guess-indian-gender' How to remove the ModuleNotFoundError: No module named 'guess-indian-gender' error? Thanks   Hi
ModuleNotFoundError: No module named 'can-you-guess'
ModuleNotFoundError: No module named 'can-you-guess'  Hi, My... 'can-you-guess' How to remove the ModuleNotFoundError: No module named 'can-you-guess' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'guess_language-spirit'
ModuleNotFoundError: No module named 'guess_language-spirit'  Hi...: No module named 'guess_language-spirit' How to remove the ModuleNotFoundError: No module named 'guess_language-spirit' error? Thanks   Hi
ModuleNotFoundError: No module named 'guess_my_x'
ModuleNotFoundError: No module named 'guess_my_x'  Hi, My Python... 'guess_my_x' How to remove the ModuleNotFoundError: No module named 'guess_my_x' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'guess-indian-gender'
ModuleNotFoundError: No module named 'guess-indian-gender'  Hi, My... named 'guess-indian-gender' How to remove the ModuleNotFoundError: No module named 'guess-indian-gender' error? Thanks   Hi
ModuleNotFoundError: No module named 'guess_language-spirit'
ModuleNotFoundError: No module named 'guess_language-spirit'  Hi...: No module named 'guess_language-spirit' How to remove the ModuleNotFoundError: No module named 'guess_language-spirit' error? Thanks   Hi
ModuleNotFoundError: No module named 'guess-indian-gender'
ModuleNotFoundError: No module named 'guess-indian-gender'  Hi, My... named 'guess-indian-gender' How to remove the ModuleNotFoundError: No module named 'guess-indian-gender' error? Thanks   Hi
buzz number
buzz number  buzz number
prime number
prime number  get one number and check the number prime or not using if statement   Java Check Prime Number
the number of parameters
the number of parameters  How do I find out the number of parameters passed into function
Number in array
Number in array  How to write a program to check the number that we are entered from keyboard is in the given array
prime number
prime number  check prime number using if statement   Java Check Prime Number
prime number
prime number  check prime number using if statement   Java Check Prime Number
prime number
prime number  To check a number entered by user is prime or not?   Java check prime number import java.util.*; class CheckPrimeNumber { static boolean isPrime(int number){ boolean isPrime=false
prime number
prime number  could u pls help me out in finding whether a number is a prime number or not?   import java.util.*; class CheckPrimeNumber { static boolean isPrime(int number){ boolean isPrime=false
prime number
prime number  To check a number entered by user is prime
Armstrong number
Armstrong number  I want java program for Armstrong number
prime number
prime number  write a java program for prime number
prime number
prime number  to check wethere given number is prime or not?  ... { static boolean isPrime(int number){ boolean isPrime=false; int i=(int) Math.ceil(Math.sqrt(number)); while (i>1
prime number
prime number  HI!I want a java program that accepts a number from user and displays whether it is a prime number or not using BufferedReader
Count number of "*"
Count number of "*"  I have this code to count the number of * from a string entered. but I need to find it from an text file. Any idea? import...++; } } System.out.println("The number of vowels in the given sentence
Number Pyramid
Number Pyramid  Hi Everyone, Can someone help me write the code for the following number pyramid: print... 0 0 0 0 4 ... "); There is 5 spaces in between each number
number sorting
number sorting  program to sort a list of numbers in descending order where the inputs are entered from keyboard in runtime   Hi Friend, Try the following code:ADS_TO_REPLACE_1 import java.util.*; class
random number
random number   Please How do I generate a program that gives me random integer from 115 to 250? Using java.util.random. Thank you very much!   Hi Friend, Try the following code:ADS_TO_REPLACE_1 import java.util.
number pyramid
number pyramid   how to get the code for the following : 1 123 12345 1234567   class Pyramid{ public static void main(String args[]){ for(int i=1;i<=7
Primary number
Primary number  write a program to find the 100th or nth primary number?   import java.util.*; class PrimeNumber{ public static void main(String a[]) { Scanner input=new Scanner(System.in
Primary number
Primary number  write a program to find the 100th or nth primary number?   import java.util.*; class PrimeNumber{ public static void main(String a[]) { Scanner input=new Scanner(System.in
VoIP Number
VoIP Number VoIP Numbers With a VoIPtalk Incoming number, callers can reach you by calling a UK number no matter where you are, Simply plug... connection and anyone can reach you by dialling your individual number. The VoIPtalk
to read number from user
to read number from user  1)print the positive equivalent of the number 2) check if the no is integer or not 3) find the square root of the number 4) find the cube of the number
number - Java Interview Questions
number  apka number kya hai aap apna number mera id per send ker do
NUMBER OF VOWELS IN A WORD
NUMBER OF VOWELS IN A WORD  WRITE A PROGRAM THAT WILL INPUT A WORD THEN IT WILL DETERMINE THE NUMBER OF VOWELS PRESENT IN THE INPUTTED WORD
using random number
using random number  generate a 10 digit number using random number and display the length of longest increasing series
To check a palindrome number?
To check a palindrome number?  program to check a number is palindrome or not using recursion?(wihout converting to string
ModuleNotFoundError: No module named 'is-number'
ModuleNotFoundError: No module named 'is-number'  Hi, My Python... 'is-number' How to remove the ModuleNotFoundError: No module named 'is-number' error? Thanks   Hi, In your python environment you
port number - JavaMail
port number  i want james2.1.3 port number like microsoft telnet>open local host _______(some port number) i wnat that port number
finding divisors of a number
finding divisors of a number   write a c program to input a number and print all its divisors using while loop
finding divisors of a number
finding divisors of a number   write a c program to input a number and print all its divisors using while loop
finding divisors of a number
finding divisors of a number   write a c program to input a number and print all its divisors using while loop

Ads