Several getKey()

Hi..
I have problem for the keypad.getKey(). Here are the program.

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char answer;
int correct;
int wrong;
long i;
long j;

void setup()
{
  //lcd.begin(16,2);
  //lcd.clear();
  Serial.begin(9600);
  //Serial.print(254, BYTE);
  //delay(100);
  Serial.println("Welcome to MATH GAME!!!");  // Initial dispaly
  Serial.println("Press * to start");         // Message on lcd
  //delay(2000);
  
  
}

void loop()
{ 
  char key = keypad.getKey();
  
  if (key != NO_KEY)
  {
    switch (key)     
    {
      case '*':
            Serial.println("[1] Addition");
            Serial.println("[2] Subtraction");
            Serial.println("[3] Exit");
            Serial.println("Choose"); 
            delay(1000);
            break;  
        
      default:
          Serial.println("Please Press *");
    }
  }
  key = keypad.getKey();
            if (key != NO_KEY)
            {
              switch (key)
              {
                  case '1':
                    //Serial.println("ONE");
                    Addition();
                    break;
                    
                  case '2':
                    Serial.println("Subtraction");
                    break;
                    
                  default:
                    Serial.println("Nothing");
              }
            }    
}

void Addition()
{
  i = random(10);
  j = random(10); 

  if ((i<9) && (j<9))
  {
      Serial.print(i);
      Serial.print("+");
      Serial.print(j);
      Serial.print("=");
      answer=i+j;
      
      char key = keypad.getKey();
      if (key != NO_KEY)
      {
        if (key == answer)
          Serial.println("CORRECT");
        else
          Serial.println("WRONG");
      }
  }
  //Serial.println(answer);
  
}

The problem is for the "Press * to start", when I press *, the output is
[1] Addition
[2] Subtraction
[3] Exit
Choose

Then I choose 1, the output is Please Press *. I press 1 for three times..only the third one, the output is correct (0+2)
Please Press *
Please Press *
0+2

Then when I entered the answer, 2, the output is Subtraction. What I want is the output should be "CORRECT".
Actually I do not know how this program works and why this happen?..

Don't do two getKeys. The program doesn't wait you know, so unless you hit the second key within about 1 millisecond of the first (and the debounce would stop that anyway) then you won't get into the second switch statement. You want something like:

void loop()
{ 
  char key = keypad.getKey();

  switch (key)     
  {
  case '*':
    Serial.println("[1] Addition");
    Serial.println("[2] Subtraction");
    Serial.println("[3] Exit");
    Serial.println("Choose"); 
    break;  

  case '1':
    //Serial.println("ONE");
    Addition();
    break;

  case '2':
    Serial.println("Subtraction");
    break;

  }  // end of switch
}  // end of loop

Don't put a default in. The default case will be no keypress.

And you need to revamp how you to the addition test. For example, what if you randomly generate 8 and 6? The answer is 14, and you can't get that back with a single kepress.

So, I cant have answer with two digits..
But, in Addition(), how can I capture the key press by user, is it correct to use if (key == answer)?

No. One is a binary number, one is an ASCII string. Put it this way, if the question was 2 + 3, then the answer is 5. But you are testing for "5" from the keypad. That is really 0x35 rather than 0x05.

I have another question.. Can I have keypad.getKey() in if statement or switch?

Absolutely. Why not test it and see? However this should work:

void loop()
{ 

  switch (keypad.getKey())     
  {
  case '*':
// nom nom nom

this is work..

void loop()
{ 

  switch (keypad.getKey())     
  {
  case '*':
// nom nom nom

but for the next switch, when I press 4 or other key, nothing happen..so I assume that this getKey() not working in switch..

key = keypad.getKey();
      if (key != NO_KEY)
      {
        switch (key)
        {
          case '1':
              i = random(4);
              j = random(5);
              if ((i<=1 && j<=5) || (i<=4 && j<=1))
              {
                lcd.setCursor(0,1);
                lcd.print("You Choose Add");
                delay(2000);
                lcd.clear();
                lcd.setCursor(1,0);
                lcd.print("What is 1+3=");
                 // when I press 4 or other key, nothing happen..
                 // so I assume that this getKey() not working in switch..
                key = keypad.getKey();
                if (key != NO_KEY)
                {
                  if (key == '4')
                  {
                    lcd.setCursor(1,0);
                    lcd.print(key);
                    delay(2000);
                    lcd.clear();
                    lcd.setCursor(0,1);
                    lcd.print("Great Job!!!");
                    correct++;
                  }
                  else
                    lcd.print("Sorry.. try again");

and one more thing..How to assign answer so that I can make if (key == answer)?
example,

int answer = 0;
int i, j;
i=random(4);
j=random(5);
lcd.print(i);
lcd.print("+");
lcd.print(j);
answer=i+j;
key = keypad.getKey();
                if (key != NO_KEY)
                {
                  if (key == answer) // how to do here?
                    //print something..

meowcat:
but for the next switch, when I press 4 or other key, nothing happen..so I assume that this getKey() not working in switch..

key = keypad.getKey();

if (key != NO_KEY)
     {
       switch (key)
...
                  // when I press 4 or other key, nothing happen..
                // so I assume that this getKey() not working in switch..
               key = keypad.getKey();

I said before "don't do two getKeys". You are doing two getKeys. Please review my earlier answer as to why.

As for the answer, you could turn the answer into ASCII like this:

answer  = i + j + '0';

Another approach, which would keep much of your existing design, is to make the getKey "block" like this.

Change:

 key = keypad.getKey();

to:

do {
  key = keypad.getKey();
 } while (key == NO_KEY);

That makes the program wait in that spot until you actually press a key. Then you have a valid key to process.