Answer:
String simonSays = "ABCDEFGHIJ";
System.out.println("Simon says: " + simonSays);
int userScore = 0;
System.out.print("Make your guess: ");
Scanner obj = new Scanner(System.in);
String guess = obj.next();
for(int i=0; i<10; i++){
if(guess.charAt(i) != simonSays.charAt(i)){
break;
}
else{
userScore++;
}
}
System.out.println("Your score is " + userScore);
Explanation:
Even though programming language is not specified, variable <em>userScore</em> implies that it should be in Java. Also, note that this code should be in your main function.
- <em>simonSays</em> variable is created to hold what Simon says and it is printed out.
- <em>userScore</em> variable is created to track user's score.
- <em>guess </em>variable takes the input written by the user. (I assumed users enter their choice. Otherwise, you should delete the Scanner part and just create another variable like String guess = "ABDFFHHH" )
- Then, we need to check if the user input is same as what Simon says using for loop. (Note that the loop is iterated 10 times because it is known Simon says 10 characters). if(guess.charAt(i) != simonSays.charAt(i), <em>charAt(i) </em>method is used to check each of the characters.
- If the characters are not same, the loop is terminated.
- If characters are same, <em>userScore</em> is increased by 1.
- After comparing all the characters, <em>userScore</em> is printed.