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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top