Solving 'Simon Says': Debugging a For Loop Problem

  • Context: MHB 
  • Thread starter Thread starter lypena35
  • Start date Start date
  • Tags Tags
    Debugging Loop
Click For Summary
SUMMARY

The forum discussion centers on debugging a Java for loop in the "Simon Says" memory game, where the user must match a sequence of characters. The user initially encountered issues with incorrect scoring due to the improper use of the continue statement. The solution involved replacing the continue statement with an else clause that triggers a break upon a mismatch. The corrected code successfully calculates the userScore based on the comparison of the simonPattern and userPattern strings.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of for loops and conditional statements
  • String manipulation in Java
  • Basic debugging techniques in Java
NEXT STEPS
  • Explore Java String methods for enhanced string manipulation
  • Learn about Java debugging tools and techniques
  • Study the implementation of user input handling in Java
  • Investigate game development concepts in Java
USEFUL FOR

Java developers, students learning programming, and anyone interested in game development or debugging Java applications.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K
Replies
1
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K