java - Simon Says counter not working properly -


i'm coding simon says game supposed keep track of how many times user completes task. code runs, output incorrect. expected output 4 code puts out 8.

import java.util.scanner;  public class simonsays  {     public static void main (string [] args)      {         string simonpattern = "";         string userpattern = "";         int userscore = 0;         int = 0;          userscore = 0;         simonpattern = "rrgbryybgy";         userpattern  = "rrgbbrybgy";          char s;         char u;          (i = 0; < 10; i++) {             s = simonpattern.charat(i);             u = userpattern.charat(i);             if (s == u)              {                 userscore = userscore + 1;                 continue;             }         }          system.out.println("userscore: " + userscore);         return;     } } 

as said in question output 4 code puts out 8. want how many continuous character matched start of string.

it means want break loop after conscative

    simonpattern = "rrgbryybgy";     userpattern  = "rrgbbrybgy"; //index 4 have different characters .   if (s == u)     {     userscore = userscore + 1; //add counter 1 @ each time character matched.      continue;   //directly move loop control statement.   } 

this piece of code check till last element present in string , counter repeatedly increases till last element of string.

try code. work

if (s != u)             {            break; //this break loop in after character doesn't matched.           }         ++userscore; // increase counter if matched. 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -