mardi 26 janvier 2021

How to refactor hundreds of conditions in chain without using if and switch statements for each cases? [closed]

I am developing an AI text communication engine, and I was wondering if anyone point me in the direction of a more efficient approach to validating user input other than just switch / if statements.

This is the foundation of it:

void Update(){
    string s = Console.Read();

    if (s.ToLower() == "c1"){
        // do 1
    }
    else if (s.ToLower() == "c2"){
        // do 2
    }

    ...

    else if (s.ToLower() == "c9342"){
        // do 9342
    }
}

I should add, I have the ability to check for keywords in the sentence.

I feel like due to the fact that all input is strings, and it is dealing with language, this may be the only way to go, but if anyone has any better approach eg. interfaces, custom types, reflection, threading or anything then I am all ears.

Thanks, Andy

Aucun commentaire:

Enregistrer un commentaire