This site requires JavaScript, please enable it in your browser!
Greenfoot back
cycv5
cycv5 wrote ...

2018/3/11

Typing Game: pop() the first word in a stack one at a time

cycv5 cycv5

2018/3/11

#
In the game, I have created a stack of strings.
Stack<String> lines = new Stack<String>();
What I want to achieve is to display the first String in this stack and wait for the user's input. The user's input is stored in another String, and I can compare those two. However, if I put the lines.pop() in the act(); it pops a string in each act, so it's going through my entire list very fast. What can I do to make it pop and stays on the screen until the reader inputs the right string, compare, and then pop() the next String in my stack.
danpost danpost

2018/3/11

#
You cannot pop the elements from the stack unconditionally if you want the list to retain anything. The command to pop needs to be in an 'if' block or something similar.
You need to login to post a reply.