asked 170k views
4 votes
I am stuck on this.... it is Java

Description:
"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. Assume simonPattern and userPattern are always the same length. Ex: The following patterns yield a userScore of 4:
simonPattern: RRGBRYYBGY
userPattern: RRGBBRYBGY

I am stuck on this.... it is Java Description: "Simon Says" is a memory-example-1
asked
User Guyts
by
7.7k points

1 Answer

6 votes

String simonPattern = "RRGBRYYBGY";

String userPattern = "RRGBBRYBGY";

int userScore = 0;

for (int i = 0; i < simonPattern.length(); i++) {

if (simonPattern.charAt(i) == userPattern.charAt(i)) {

userScore++;

} else {

break;

}

}

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

answered
User Koen Lostrie
by
7.9k points