You need to wait to change the pair of numbers until after the user has entered his answer.

You need to wait to change the pair of numbers until after the user has entered his answer.

how can I do this in code?
Could you explain me in codes, please?

Where in your code do you change the value BEFORE it is compared to the user's value?
Move that code so it is AFTER the compare.

Where in your code do you change the value BEFORE it is compared to the user's value? Move that code so it is AFTER the compare.

I not sure, what codes should I move in...

answer = numb1 + numb2;

Are those codes?
and Where should I move them in?
Do you mean move them to paint()?

You need to go through your code in the order that it executes to see where the values should be set and where they should be tested.
If you add lots of printlns that show the values of the variables as they are changed and where they are tested, it will help you see how the code needs to be done.

Make a list of the events as they are supposed to happen. Something like this:
1) set values for numb1 and numb2
2) show the values for the user to see
3) ....

When you have a list of how the steps in your program should execute, them look at the code in your program to see if it is following those steps.

You need to go through your code in the order that it executes to see where the values should be set and where they should be tested.
If you add lots of printlns that show the values of the variables as they are changed and where they are tested, it will help you see how the code needs to be done.

Make a list of the events as they are supposed to happen. Something like this:
1) set values for numb1 and numb2
2) show the values for the user to see
3) ....

When you have a list of how the steps in your program should execute, them look at the code in your program to see if it is following those steps.

Ok, I did,what you tell me,and now answer(result of addition) value is 0(nothing added yet);but draw face is still appering after the question. Don't let me add the input. This is the printlns before add any number to textbox.

[TEX]What is 6 + 9?[/TEX]
[TEX]Face: Player Answer= 0 Answer= 0[/TEX]
[TEX]Smile Face: Player Answer Ok[/TEX]
[TEX]Face: Player Answer= 0 Answer= 0[/TEX]
[TEX]Smile Face: Player Answer Ok[/TEX]

I just can guess, this is because paint()method is always call before an actionPerformed() method. I also add the repaint() inside of actionPerformed().
This is my only problem now. Thank you for your time.

Please list here the events of the program as I asked you to do in my last post.
You need to see the order of what needs to happen in your code.
Then we'll work on the list of events to get them in the correct order.
When we have that, THEN you can rewrite your code to work as the design we have made says it should work.

If you do not design how the program is supposed to work, you will have a hard time making it do what you want to do. Design first, then code.

Please list here the events of the program as I asked you to do in my last post.
You need to see the order of what needs to happen in your code.
Then we'll work on the list of events to get them in the correct order.
When we have that, THEN you can rewrite your code to work as the design we have made says it should work.

1) Set values for numb1 and numb2
2) Show the values for the user to see
3) User enter a value.
4) numb1 and numb2 are added in order to get an answer amount.
5) Comparation between user's value and answer amount.
6) If user value is correct:
Display a message "Correct answer"
Display happy face.
7) If user value is incorrect:
Display a message "Incorrect answer"
Display sad face.
8)Start again from 1st event.

That looks reasonable.
Is the last posted version of your program the current one or do you need to post the new code so we have the current version to look at?

Is the last posted version of your program the current one or do you need to post the new code so we have the current version to look at?

A new post is better.

private void resetLabel() 
    {
        numb1 = (int) (Math.random() * 10) + 1;
        numb2 = (int) (Math.random() * 10) + 1;
        
        prompt.setText("What is " + numb1 + " + " + numb2 + "?");
        System.out.println("What is " + numb1 + " + " + numb2 + "?");
    }
public void actionPerformed(ActionEvent e) 
    {
        
       player = Integer.parseInt(input.getText());
       input.setText("");
       System.out.println("User value"+ player); 
       
       System.out.println("If Statement:");
       System.out.println("Player Answer= " + player + " Answer= " + answer);
       
       answer = numb1 + numb2;

        if (player == answer )
        {    
          System.out.println("Confirm answer:");
          System.out.println("Player Answer Ok");
            status.setText("Excellent");
            resetLabel();
            input.setText("");
        } 

        else 
        {
           System.out.println("Confirm answer:");
           System.out.println("Player Answer Not Ok");
           status.setText("Incorrect answer, please try again");
           resetLabel();
           input.setText(""); 
        }
          repaint();
     }
public void paint(Graphics g) 
    {
       System.out.println("Face: Player Answer= " + player + " Answer= " + answer);
       if (player == answer)//Draw a smile face every time answer is correct in addition
        {
            System.out.println("Smile Face: Player Answer Ok");
            g.drawOval(150, 85, 71, 90);
            g.drawArc(170, 130, 30, 30, 0, -180);
            g.drawOval(172, 118, 10, 10);
            g.drawOval(190, 118, 10, 10);
        }

        else 
        {
            System.out.println("Confirm answer:");
            System.out.println("Sad Face: Player Answer Not Ok");
            g.drawOval(150, 85, 71, 90);
            g.drawArc(170, 140, 30, 30, 0, 180);
            g.drawOval(172, 118, 10, 10);
            g.drawOval(190, 118, 10, 10);
        }
    }

You need to post the complete program so it can be seen where every event in the list of events is happening.

You need to post the complete program so it can be seen where every event in the list of events is happening.

Ok!

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Game extends Applet implements ActionListener {

    Label prompt;
    TextField input;
    Label status;
    int numb1;
    int numb2;
    int player;
    int answer ;
    private Font font1;
  
    public void init() {
        prompt = new Label("");
        add(prompt);
        resetLabel();
        prompt.setFont(new Font("Dialog", Font.PLAIN, 15));
        prompt.setForeground(new Color(16711960));
        
        input = new TextField(10);
        add(input);
        player = 0;
        input.setFont(new Font("Dialog", Font.PLAIN, 15));
        input.setForeground(new Color(16711960));
        

        status = new Label("Status Here");
        add(status);
        status.setFont(new Font("Dialog", Font.PLAIN, 14));
        status.setForeground(new Color(16711960));
        
        
        font1 = new Font("", Font.BOLD, 16);
        
        input.addActionListener(this);
    }

    private void resetLabel() 
    {
        numb1 = (int) (Math.random() * 10) + 1;
        numb2 = (int) (Math.random() * 10) + 1;
        
        prompt.setText("What is " + numb1 + " + " + numb2 + "?");
        System.out.println("What is " + numb1 + " + " + numb2 + "?");
    }
    
  public void actionPerformed(ActionEvent e) {
        
       player = Integer.parseInt(input.getText());
       input.setText("");
       System.out.println("player"+ player); 
       
       System.out.println("If Statement:");
       System.out.println("Player Answer= " + player + " Answer= " + answer);
       
       answer = numb1 + numb2;
        if (player == answer )
        {    
          System.out.println("Confirm answer:");
          System.out.println("Player Answer Ok");
            status.setText("Excellent");
            resetLabel();
            input.setText("");
            
        } 
        else {
           System.out.println("Confirm answer:");
           System.out.println("Player Answer Not Ok");
           status.setText("Incorrect answer, please try again");
           resetLabel();
           input.setText(""); 
           
        }

    repaint();   

    }
  
  public void paint(Graphics g) 
    {
        setBackground(Color.red);
        g.setColor(Color.pink);
        g.fillOval(250, 95, 175, 55);
        g.setColor(Color.yellow);
        g.setFont(font1);
        g.drawString("Math Guessing Game", 255, 130);
      
       System.out.println("Face: Player Answer= " + player + " Answer= " + answer);
       if (player == answer)//Draw a smile face every time answer is correct in addition
        {
            System.out.println("Smile Face: Player Answer Ok");
            g.drawOval(150, 85, 71, 90);
            g.drawArc(170, 130, 30, 30, 0, -180);
            g.drawOval(172, 118, 10, 10);
            g.drawOval(190, 118, 10, 10);
            g.setColor(Color.white);
  
        }
       
        else {
            System.out.println("Confirm answer:");
            System.out.println("Sad Face: Player Answer Not Ok");
            g.drawOval(150, 85, 71, 90);
            g.drawArc(170, 140, 30, 30, 0, 180);
            g.drawOval(172, 118, 10, 10);
            g.drawOval(190, 118, 10, 10);
            g.setColor(Color.white);
        }

    }

   
}

Can you explain what the problem is with the code you just posted?
It works for me.

Can you explain what the problem is with the code you just posted?
It works for me.

The draw (face) appears before user adds the value, at the start when program has been ran.

What do you want displayed when the program starts?
The paint method currently only displays one of two things. If you want it to display something else, you need to add code so the paint method knows what to display.
You will need the paint method to be able to display one of three different things.

What do you want displayed when the program starts?
The paint method currently only displays one of two things. If you want it to display something else, you need to add code so the paint method knows what to display.
You will need the paint method to be able to display one of three different things.

What I want is display at the start ONLY this: fillOval & drawString

setBackground(Color.red);
        g.setColor(Color.pink);
        g.fillOval(250, 95, 175, 55);
        g.setColor(Color.yellow);
        g.setFont(font1);
        g.drawString("Math Guessing Game", 255, 130);

THEN to draw the face(smile or sad) AFTER Comparation between user's value and answer amount.
What kind of codes should I use in this situation?
Thanks

You could use what I call one time code.
Have a boolean class variable that starts as true.
In the paint method test if the flag is true,
if the flag is true:
set it false.
do the things you want to do just this one time
return/exit from the paint method
else
do what you want done on later calls to paint.

A problem could occur if paint is called several times before the user enters data.
Your code might have to detect if the user has ever entered anything. If the user has never entered any data, then display the initial screen.

Have a boolean class variable that starts as true.
In the paint method test if the flag is true,
if the flag is true:
set it false.
do the things you want to do just this one time
return/exit from the paint method
else
do what you want done on later calls to paint.

I set the boolean class variable that starts as true, BUT what do you mean about FLAG and RETURN/EXIT method? I'm new in those terms, could you explain me this, please?
Thanks.

A flag is a variable that is used to remember a program state.
The most common one is a boolean. Ask Google for its definition.

return is the java statement that exits execution flow from the current method.

Have a boolean class variable that starts as true.
In the paint method test if the flag is true,
if the flag is true:
set it false.
do the things you want to do just this one time
return/exit from the paint method
else
do what you want done on later calls to paint.

I made those changes, But it doesn't work as I want. I've highlighted them in red colour the new codes. Could you tell me what am doing wrong? Thanks.

public class Game extends Applet implements ActionListener {
boolean flag = true;
}
if (flag = true)
     {
       System.out.println("Face: Player Answer= " + player + " Answer= " + answer);
       if (player == answer)//Draw a smile face every time answer is correct in addition
        {
            flag=false;
            System.out.println("Smile Face: Player Answer Ok");
            g.drawOval(150, 85, 71, 90);
            g.drawArc(170, 130, 30, 30, 0, -180);
            g.drawOval(172, 118, 10, 10);
            g.drawOval(190, 118, 10, 10);
            g.setColor(Color.white);
        }
      if (flag)return;
       
        else {
            System.out.println("Confirm answer:");
            System.out.println("Sad Face: Player Answer Not Ok");
            g.drawOval(150, 85, 71, 90);
            g.drawArc(170, 140, 30, 30, 0, 180);
            g.drawOval(172, 118, 10, 10);
            g.drawOval(190, 118, 10, 10);
            g.setColor(Color.white);
            
        }

it doesn't work as I want

What is it that you want and what does the program do?

One problem is that this statement is an assignment statement that sets the value of flag to true.

if (flag = true)

The value of a boolean does not need to be tested. You should code it like this:

if (flag)

What is it that you want and what does the program do?

What I want is: (my problem starts from 6th event, where has to be display a face.)
1) Set values for numb1 and numb2
2) Show the values for the user to see
3) User enter a value.
4) numb1 and numb2 are added in order to get an answer amount.
5) Comparation between user's value and answer amount.
6) If user value is correct:
Display a message "Correct answer"
Display happy face.
7) If user value is incorrect:
Display a message "Incorrect answer"
Display sad face.
8)Start again from 1st event.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Thankfully the draw (face) does NOT appears ANYMORE before user adds the value, at the start when program has been ran.

BUT the program doesn't draw the face(smile or sad) AFTER Comparation between user's value and answer amount.

What do you display at step 2? You have the display of the faces in RED.
What do you display BEFORE the user enters any data?
Can you detect whether the user has entered any data?
How do you know if the user has entered anything? Is there a listener called?
If the user has not entered anything yet, display the "BEFORE" drawing.
After the first entry then either the Happy or the Sad face.

Have a boolean: noUserData = true at the beginning of the program.
Set it false as soon as you get some user data.
Use that boolean to control if you display the "BEFORE" drawing or one of the faces.

What do you display at step 2?
You have the display of the faces in RED.
What do you display BEFORE the user enters any data?

just display a red background + a small graphic saying "Maths games" and the random numbers.

Can you detect whether the user has entered any data?
How do you know if the user has entered anything? Is there a listener called?

I use this listener call repaint(); inside of actionPerformed method.

If the user has not entered anything yet, display the "BEFORE" drawing.
After the first entry then either the Happy or the Sad face.

it doesn't display the before drawing. The faces aren't display after first entry.

Have a boolean: noUserData = true at the beginning of the program.
Set it false as soon as you get some user data.
Use that boolean to control if you display the "BEFORE" drawing or one of the faces.

The boolean controls the BEFORE draw, BUT DOESN'T DISPLAY THE AFTER first entry.

Your logic must be confused. I'd expect somthing like this:

if(noUserInput) {
  // draw the BEFORE images
  return; // exit the paint method
}
// If you get to here, the user has made an input
// The code following here that decides which happy face to draw
...

Some where else in your code when you detect there has been user input, you set noUseInput to false.

Your logic must be confused. I'd expect something like this:

if(noUserInput) {
  // draw the BEFORE images
  return; // exit the paint method
}
// If you get to here, the user has made an input
// The code following here that decides which happy face to draw
...

I did what you said, but it's happening the same, I mean the draw the BEFORE image is display, BUT the IF STATEMENT of happy or sad image does not be display after user input for some reason.
I'll put the code below to show you what I've done.

public void paint(Graphics g) 
    {
        boolean noUserInput = true;
        
        g.setColor(getBackground());
        setBackground(Color.red);
        if (noUserInput)
       {
        g.setColor(Color.pink);
        g.fillOval(250, 95, 175, 55);
        g.setColor(Color.yellow);
        g.setFont(font1);
        g.drawString("Math Guessing Game", 255, 130);
        //g.drawString("Your level is ", 30, 190);
        //g.drawRect(110, 200, answer, 20);
        return;
       }
       
        if(noUserInput = false)
        {
       System.out.println("Face: Player Answer= " + player + " Answer= " + answer);
       if (player == answer)//Draw a smile face every time answer is correct in addition
        {
            System.out.println("Smile Face: Player Answer Ok");
            g.drawOval(150, 85, 71, 90);
            g.drawArc(170, 130, 30, 30, 0, -180);
            g.drawOval(172, 118, 10, 10);
            g.drawOval(190, 118, 10, 10);
            g.setColor(Color.white);
       }
              
       
        else {
            
            System.out.println("Confirm answer:");
            System.out.println("Sad Face: Player Answer Not Ok");
            g.drawOval(150, 85, 71, 90);
            g.drawArc(170, 140, 30, 30, 0, 180);
            g.drawOval(172, 118, 10, 10);
            g.drawOval(190, 118, 10, 10);
            g.setColor(Color.white);
            }
    }

    }

Line 19 contains an assignment statement not an equality test.

You only need to test the boolean in one place. If it is true there the method exits.
When you get to line 19 the value must be false.

Having the definition and the setting of noUserInput as a local variable in the paint method means that it will ALWAYS ALWAYS ALWAYS be set to true when the method starts.

Move the variable outside of the paint method.

Somewhere else in your code when you detect there has been user input, you set noUseInput to false.

Line 19 contains an assignment statement not an equality test.

You only need to test the boolean in one place. If it is true there the method exits.
When you get to line 19 the value must be false.

Having the definition and the setting of noUserInput as a local variable in the paint method means that it will ALWAYS ALWAYS ALWAYS be set to true when the method starts.

Move the variable outside of the paint method.

Somewhere else in your code when you detect there has been user input, you set noUseInput to false.

I moved the variable outside.
I changed equality test instead of assignment statement

if(noUserInput == false)

I still have the same problem:
IF STATEMENT of happy or sad image does not be display AFTER user input

public void paint(Graphics g) 
    {
        g.setColor(getBackground());
        setBackground(Color.red);
        
        if (noUserInput)
       {
         g.setColor(Color.pink);
         g.fillOval(250, 95, 175, 55);
         g.setColor(Color.yellow);
         g.setFont(font1);
         g.drawString("Math Guessing Game", 255, 130);
        //g.drawString("Your level is ", 30, 190);
        //g.drawRect(110, 200, answer, 20);
         return;
       }
       
     if(noUserInput == false)
    {
       System.out.println("Face: Player Answer= " + player + " Answer= " + answer);
       if (player == answer)//Draw a smile face every time answer is correct in addition
        {
            System.out.println("Smile Face: Player Answer Ok");
            g.drawOval(150, 85, 71, 90);
            g.drawArc(170, 130, 30, 30, 0, -180);
            g.drawOval(172, 118, 10, 10);
            g.drawOval(190, 118, 10, 10);
            g.setColor(Color.white);
          //g.drawRect(110, 200, player, 20);// bar increase in lenght every time answer is correct
        }
              
       
        else 
       {
            System.out.println("Confirm answer:");
            System.out.println("Sad Face: Player Answer Not Ok");
            g.drawOval(150, 85, 71, 90);
            g.drawArc(170, 140, 30, 30, 0, 180);
            g.drawOval(172, 118, 10, 10);
            g.drawOval(190, 118, 10, 10);
            g.setColor(Color.white);
       }
       
    }

    }

Where do you set noUserInput to false?
Where do you FIRST detect that the user has entered something?

When line 18 is execute it will always be true.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.