Home > Blockchain >  How can i get these if statements not to wait for each other C#
How can i get these if statements not to wait for each other C#

Time:12-23

How do I skip the first if statement if the userinput is b.

if (Int32.TryParse(Console.ReadLine(), out int userInput) && userInput >= 0 && userInput <= 50)
{
    DeleteDirectory(delDir, true);
    CloneDirectory(saves[userInput], cloneDir);

}


if (Console.ReadLine() == "b")
{
    DeleteDirectory(saves[0], true);
    CloneDirectory(delDir, saves[0]);
}

if (Console.ReadLine() == "r")
{
    DeleteDirectory(delDir, true);
    CloneDirectory(saves[0], cloneDir);
}

I tried to use a switch case, but iv never had to put much logic into them before, and couldn't figure out a way to get the first if statement into a case. I'm pretty new to coding outside of unity, And even that I'm pretty new too. So normally I could just chuck this into the update function, and it would check all if the statements every frame, no problem. But here obviously it waits for an input, then moves on to the next if statement. I tried else if as well, but maybe I was using that wrong. Or maybe there is a better way to grab user inputs other than Console.Readline that won't wait for an answer to move on.

edit* added a link to the github so you can see all the code if that helps https://github.com/horse4lunch/High-on-life-save-manager-/tree/horse4lunch-Experimental

CodePudding user response:

Perhaps you meant to prompt the user for input once and then perform your logic on that input? For example:

var input = Console.ReadLine();

if (Int32.TryParse(input, out int userInput) && userInput >= 0 && userInput <= 50)
{
    DeleteDirectory(delDir, true);
    CloneDirectory(saves[userInput], cloneDir);
}

if (input == "b")
{
    DeleteDirectory(saves[0], true);
    CloneDirectory(delDir, saves[0]);
}

if (input == "r")
{
    DeleteDirectory(delDir, true);
    CloneDirectory(saves[0], cloneDir);
}

CodePudding user response:

You are calling Console.ReadLine() repeatedly. This waits for user input each time. Instead, read the user input into a variable once.

string input = Console.ReadLine();
if (Int32.TryParse(input, out int userInput) && userInput >= 0 && userInput <= 50) {
    DeleteDirectory(delDir, true);
    CloneDirectory(saves[userInput], cloneDir);
} else if (input == "b") {
    DeleteDirectory(saves[0], true);
    CloneDirectory(delDir, saves[0]);
} else if (input == "r") {
    DeleteDirectory(delDir, true);
    CloneDirectory(saves[0], cloneDir);
}

Since only one if can match the input, use else if. This will execute the first matching case and not evaluate the remaining cases.

But it would be easier to use a switch statement:

string input = Console.ReadLine();
switch (input) {
    case { } when Int32.TryParse(input, out int index) && index is >= 0 and <= 50:
        DeleteDirectory(delDir, true);
        CloneDirectory(saves[index], cloneDir);
        break;
    case "b":
        DeleteDirectory(saves[0], true);
        CloneDirectory(delDir, saves[0]);
        break;
    case "r":
        DeleteDirectory(delDir, true);
        CloneDirectory(saves[0], cloneDir);
        break;
}

This uses pattern matching. { } is an empty property pattern I used as a dummy case label, because we need only the when-part. It actually means input != null.

A second pattern is index is >= 0 and <= 50. very handy, as we don't have to repeat the variable name.

  • Related