100k views
4 votes
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. For each match, add one point to user_score. Upon a mismatch, end the game. Sample output with inputs: 'RRGBRYYBGY' 'RRGBBRYBGY'

User Ashy
by
5.1k points

1 Answer

7 votes

Answer:

// Here is code in java.

import java.util.*;

// class definition

class Solution

{

// main method of the class

public static void main (String[] args) throws java.lang.Exception

{

try{

// create two string and initialize them

String simon="RRGBRYYBGY";

String user="RRGBBRYBGY";

// variable to count score

int user_score=0;

// count the score of both strings

for(int i=0;i<10;i++)

{

if(simon.charAt(i)==user.charAt(i))

user_score++;

// if there is mismatch, loop breaks

else

break;

}

// print the score

System.out.println("user score:"+user_score);

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

Create two string variables and initialize first with simon sequence and second with user sequence. Create a variable "user_score" to count the score of memory game.In the for loop, check each character of first string with Corresponding character of second string.If there is match then add 1 to user_score and if mismatch break the loop.Then print the user_score.

Output:

user score:4

User Nakrill
by
4.7k points