First: the init() method is the equivalent of a constructor for an applet, as such it gets called first. Since you don't initialize your numbers before using them in the labels, Java takes them as 0 and displays it that way.
Second: the problem lies in your actionPerformed method. The user types something and hits enter, your actionPerformed method is called. Instead of first checking if the user gave the correct answer, you first change the numbers, and check afterwards if the user gave the correct answer to a question he couldn't see when he answered. Unless by chance you generate numbers that give the same answer, the user will always have the wrong answer, thereby never getting through your if statement. And why is there a for loop? Since the user can't try another time (you just use e.getActionCommand(), which doesn't change if your loop), the loop just checks something 3 times without anything having changed.
Lastly: What do you mean with "doesn't end"? Are the sentences of your if-else statement being printed constantly or is nothing happening? Put some System.out.printlns in your code to see where the program is at various stages.
Aviras
Junior Poster in Training
87 posts since Jul 2008
Reputation Points: 24
Solved Threads: 8
Skill Endorsements: 0
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
is that a problem? I don't really check the forum at java-forums, and I assume there will be more members who don't.
stultuske
Industrious Poster
4,372 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
No problem. Just trying to save people time by showing other posts of this question by the OP.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
To count the number of times the user pressed Enter you need to have a counter variable at the class level that you increment every time the user presses Enter and goes to the actionPerformed method.
to repeat the question to user 3 times
You can NOT loop inside of that method because it is called in response to the user's pressing Enter. The "loop" needs to consider all the events that you want to happen in the "loop": Show numbers, get user response, test response and display results.
You can NOT get a new user response while in the actionPerformed method. You must exit and wait for the user to enter the new number and press Enter.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Since you're making a program which responds to the user doing things, instead of the user doing things the program tells it to do, a for loop is not very apropriate here. A normal say console program, is one thread, your code is executed 'top to bottom'. An applet waits for the user to do something and then reacts to it.
As NormR1 said, keep a variable that you increment each time the actionPerformed gets called, and at the end of that method, you check to see if the number of tries has been reached, and your program can respond to it.
Aviras
Junior Poster in Training
87 posts since Jul 2008
Reputation Points: 24
Solved Threads: 8
Skill Endorsements: 0
define an int at the class level:
int tryCount = 0; // counts number of user tries
in the actionPerformed method:
...
tryCount++; ?? count the number of tries
if(tryCount > MaxNbrTries) { // test if max tries done yet
// Do what you want when max # exceeded
}
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Can you post the complete code for the version you are now working with?
Its hard to tell what your program is doing with only these small bits of code.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
What is supposed to happen when I execute the code and type in a number and press enter?
If what I type in is correct, "Excellent" is displayed and "ans ok" is printed on console.
If it is not correct, "incorrect answer" is displayed. Nothing is printed on the console.
The happy face is always shown.
Look at your logic to see why the "ans ok" message is printed in one case and not printed in another.
Then look at when the happy face is drawn.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Your code always draws the happy face. Change the logic of the code so that it only draws the face when you want it drawn instead of always drawing it.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
One problem debugging your code is that your printlns do not show where there are being printed. You should change the text of what is printed out so that you know exactly which println printed it.
When you change the printlns so you can see which one printed you will see that there is a problem with when you set values and when you test them.
Then you will have to think about the how your application works to be sure the variables have the right values when you test them.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16