•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,637 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,193 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 218 | Replies: 5 | Solved
![]() |
•
•
Join Date: Jul 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
I'm having a problem with my assginment. This is the Java Number Guessing Game. It compiles and runs with no errors but when executed it stops with only one user input. You must hit the "Play Again" button to continue. Here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.event.*;
public class GuessNumber extends JApplet implements ActionListener{
private String titleStr="I have a number between 1 and 1000. Can you guess my number?";
private JLabel title=new JLabel(titleStr);
private JLabel ask=new JLabel("Please enter your first guess: ");
private JTextField guess=new JTextField();
private JLabel resultLabel=new JLabel("Result: ");
private JLabel result=new JLabel();
private JButton replay=new JButton("Play again");
Container cp=getContentPane();
private int randomNum;
public void init() {
randomNum=new Random().nextInt(1000)+1;
guess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
try {
int guessNum=Integer.parseInt(str);
if (guessNum<randomNum) {
result.setText("Too Low");
guess.setBackground(Color.BLUE);
}
if (guessNum>randomNum) {
result.setText("Too High");
guess.setBackground(Color.RED);
}
if (guessNum==randomNum) {
result.setText("Correct!");
guess.setBackground(Color.WHITE);
guess.setEditable(false);
}
} catch (Exception ex) {
result.setText(ex.toString());
}
}
});
replay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
randomNum=new Random().nextInt(1000)+1;
guess.setEditable(true);
guess.setText("");
guess.setBackground(Color.WHITE);
result.setText("");
}
});
JPanel midScreen=new JPanel();
midScreen.setLayout(new GridLayout(2,2));
midScreen.add(ask); midScreen.add(guess);
midScreen.add(resultLabel); midScreen.add(result);
cp.setLayout(new FlowLayout());
cp.add(title);
cp.add(midScreen);
cp.add(replay);
}
public static void main(String[] args) {
JApplet applet = new GuessNumber();
JFrame frame = new JFrame("Guess my number");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.getContentPane().add(applet);
frame.setSize(400,150);
applet.init();
applet.start();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
} no it doesn't, just keep putting in numbers and pressing enter, it works fine.
what problem are you having?
what problem are you having?
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
•
•
Join Date: Jul 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
no it doesn't, just keep putting in numbers and pressing enter, it works fine.
what problem are you having?
I had hoped that the user would backspace and enter a new number. As it works now the user can only continue to input numbers after each previouis number until it throw the exception.
huh? backspace and entering a new number works fine, it gives the too high or too low message every time i press enter, and when i get it right it displays correct, hit new game it picks a new number, close the window exits the VM
i cannot reproduce what your saying, although, i would suggest clearing the text area when the user presses enter and adding a guess counter, just because i would you don't have to,
have you tried stepping through it in the debugger?
i cannot reproduce what your saying, although, i would suggest clearing the text area when the user presses enter and adding a guess counter, just because i would you don't have to,
have you tried stepping through it in the debugger?
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
no problem
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the Java Forum
- Previous Thread: Array
- Next Thread: linked list help


Linear Mode