dimanche 4 août 2019

kotlin function with for loop and if statement does not return a different value twice

I have created a function for getting a match from an input,it has a "count" variable that helps me keep track of the match, however it only works once, the second time is used it fails, I dont know if probably the values mantain and because of that the match cant be found, heres the code:

PD: when I run the program here is the result: Anything Not a match

   class Class1(var self: String,var tipo: String)       
   var test_class = Class1("","")       

   fun giver(){       
       test_class.self = "Anything"       
       test_class.tipo = "Something"       
   }       

   class Funciones(){       
    fun match_finder(texto: String): Any{       
        var lista = listOf<String>(test_class.self,test_class.tipo)       
        var lista_de_listas = listOf<String>("test_class.self","test_class.tipo")       
               var count = -1       
               var variable = ""       
               for (i in lista_de_listas){       
                   count = count + 1       
                   if (texto == i){       
                       lista_de_listas = lista       
                       var variable = lista_de_listas[count]       
                       return variable       

                   } else {       
                       return "Not a match"       
                   }       
               }       
              return variable       
           }        
       }           

   fun main(){       
       giver()       
       var x = "test_class.self"       
       var z = "test.class.tipo"       
       var funcion = Funciones()       
       var y = funcion.match_finder(x)       
       var c = funcion.match_finder(z)       
       println(y)       
       println(c)       

   }

Aucun commentaire:

Enregistrer un commentaire