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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.