Solving 'Simon Says': Debugging a For Loop Problem

In summary, the problem is that the code keeps getting an error message saying that it cannot find the matching character between simonPattern and userPattern.
  • #1
lypena35
18
0
I have tried a bunch of different ways to edit this problem. The user input is 4 and I keep getting a output of 8 or 0. I appreciate the help!

The problem is:

"Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence. Create a for loop that compares the two strings starting from index 0. For each match, add one point to userScore. Upon a mismatch, exit the loop using a break statement. Ex: The following patterns yield a userScore of 4:
simonPattern: RRGBRYYBGY
userPattern: RRGBBRYBGY

My code:

Code:
import java.util.Scanner;

public class SimonSays {
   public static void main (String [] args) {
      String simonPattern = "";
      String userPattern = "";
      int userScore = 0;
      int i = 0;

      userScore = 0;
      simonPattern = "RRGBRYYBGY";
      userPattern  = "RRGBBRYBGY";     char s;
     char u;  for (i=0;i<10;i++) {
     s = simonPattern.charAt(i);
     u = userPattern.charAt(i);

     if (s==u) {
        userScore = userScore + 1;
        continue;
     }

      
      }
      System.out.println("userScore: " + userScore);

      return;
   }
}
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
You ignored the instructions about "break".
Modify your code:

Code:
if (s==u) {
  userScore = userScore +1;
  continue;
}
A corrected version:

Code:
if (s == u) {
  userScore = userScore +1;
}
else {
  break;
}

I don't quite understand what you're expected to do with regard to the origin of simonPattern and userPattern. Is simonPattern random? Is the program to read userPattern?
 
  • #3
Yes! It worked! Thank you for your help. I tried the else/break as you suggested before hand. I forgot to remove the continue and that was causing the error I was receiving. I am unsure; it's a embedded challenge in my java digital textbook. They don't give much instruction sometimes and they won't let me modify the code only the middle section. The compiler they use is also very strict which is a good thing. It is just sometimes harder to solve because of the fact that I can not modify the entire section and because the compiler's modifications or requirements change for every challenge in every subsection.
 

FAQ: Solving 'Simon Says': Debugging a For Loop Problem

What is 'Simon Says' and why is it important to solve it?

'Simon Says' is a popular game that helps improve memory and concentration skills. It is important to solve it because it can also be used to teach programming concepts, such as debugging, which is a crucial skill for any scientist or programmer.

What is a for loop and why is it used in 'Simon Says'?

A for loop is a programming construct that allows a set of instructions to be repeated for a certain number of times. In 'Simon Says', it is used to repeat the game instructions and check for correct user input.

What are some common problems that can occur in a for loop while playing 'Simon Says'?

Some common problems that can occur in a for loop while playing 'Simon Says' include incorrect syntax, infinite loops, and incorrect variable initialization or incrementation.

How can I debug a for loop problem in 'Simon Says'?

To debug a for loop problem in 'Simon Says', you can use tools such as print statements or a debugger to track the values of variables and identify where the code is going wrong. It is also helpful to break the problem down into smaller parts and test each part separately.

Are there any tips for avoiding for loop problems in 'Simon Says'?

Yes, some tips for avoiding for loop problems in 'Simon Says' include carefully checking the syntax, making sure the loop has a clear and defined end condition, and testing the code with different inputs. It is also helpful to plan and pseudocode the logic of the for loop before writing the actual code.

Similar threads

Back
Top