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);
Step-by-step explanation:
Even though programming language is not specified, variable userScore implies that it should be in Java. Also, note that this code should be in your main function.
- simonSays variable is created to hold what Simon says and it is printed out.
- userScore variable is created to track user's score.
- guess 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), charAt(i) method is used to check each of the characters.
- If the characters are not same, the loop is terminated.
- If characters are same, userScore is increased by 1.
- After comparing all the characters, userScore is printed.