Solving 'Simon Says': Debugging a For Loop Problem

  • Context:
  • Thread starter Thread starter lypena35
  • Start date Start date
  • Tags Tags
    Debugging Loop
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 11K views
lypena35
Messages
18
Reaction score
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:
Physics news on Phys.org
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?
 
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.