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

  • Thread starter Thread starter lypena35
  • Start date Start date
  • Tags Tags
    Debugging Loop
AI Thread Summary
The discussion revolves around a coding issue in a Java program for the "Simon Says" memory game. The user is trying to compare a predefined sequence (simonPattern) with a user-provided sequence (userPattern) to calculate a score based on matching characters. The user initially encounters problems with incorrect outputs of 8 or 0 due to improper loop control. The key solution involves replacing a "continue" statement with an "else" statement that includes a "break" to exit the loop upon a mismatch. The user clarifies that the simonPattern is fixed while the userPattern is inputted, and they express frustration with the strict requirements of their coding challenge, which limits modifications to only a specific part of the code. Ultimately, the suggested changes resolved the issue, allowing the program to function correctly.
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:
Technology 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top