"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: R, R, G, B, R, Y, Y, B, G, Y

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


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

return;
}
}

Respuesta :

Answer:

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

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

       {

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

           {

               userScore+=1;

           }

           else

           {

               break;

           }

           

           

       }

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

 

    }

}

Explanation:

the string method charAt() is used to access each character of the string.

Following are the C++ program to compare string value:

Program Explanation:

  • Defining header file.
  • Defining the main method.
  • Defining a two-string variable "simonPattern, userPattern" that holds a string value, and an integer variable "userScore" is declared.
  • In the next step, a for loop is declared that uses an if block that compares the string value with the "equal to" operator and incrementing the "userScore" value.
  • In the else block it break the value, and outside the loop, a print method is used that prints the calculated value of the integer variable value.

Program:

#include <iostream>//header file

#include <string>//header file

using namespace std;

int main() //main

{

  string simonPattern= "RRGBRYYBGY", userPattern = "RRGBBRYBGY";//defining string variable and assign value

  int userScore=0;//defining integer variable userScore

  for(int i = 0;i<10;i++)//defining for loop that compares the string value

  {

     if(simonPattern[i]==userPattern[i])//using if block that check string value by using equal to operator

     {

        userScore++;//incrementing userScore value

     }

     else//defining else block

     {

      break;//using break keyword

     }

  }

  cout<<"userScore: "<<userScore<<endl;//print value

  return 0;

}

Output:

Please find the attached file.

Learn more:

brainly.com/question/13720920

Ver imagen codiepienagoya