view raw
bryan callahan bryan callahan - 1 year ago 160
Java Question

Simon Says counter not working properly

I'm coding a Simon Says game that is supposed to keep track of how many times the user successfully completes the task. The code runs, but the output is incorrect. The expected output is 4 but my code puts out 8.

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;

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


So I assume you go through the characters and when the first time the characters don't match, then you will consider it "failing" and only count the preceding matches.

So in this case, you can use

if (s != u) {

to break out of the loop; otherwise, just increment the count.

I don't like to use break personally, but would just write a function to return the count when the characters don't match.