Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simon Says is a memory game where \"Simon\" outputs a sequence of 10 characters

ID: 3886454 • Letter: S

Question

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 import java.util.Scanner: public class SimonSays { public static void main (String args) { String simonPattern = ^nn: String userPattern = ^nn: int userScore =: int i = 0: userScore = 0: simonPattern = "RRGBRYYBGY": userPattern = "RRGBBRYBGY": /* Your solution goes here */ System.out.println("userScore: " + userScore): return;

Explanation / Answer

SimonSays.java

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";

/* This for loop compares each character in the simonPattern
* with each character in the userPattern
*/
for (i = 0; i < simonPattern.length(); i++) {
if (simonPattern.charAt(i) == userPattern.charAt(i)) {
//counting the score
userScore++;
} else {
break;
}
}

//displaying the userScore
System.out.println("userScore :" + userScore);
return;
}

}

_____________________

Output:

userScore :4

_____________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote