+ 2

Method that takes user input and returns a string value get's stuck

Well as I said in the title, I have a method that when's called should take user input and return another string value based on that input. For example: User inserts "A" and the method returns "a". I made it work once, but then when I try to change user's input it get's stuck and requires the user to repeat the same input. class TakeDecision{ public string Choose(){ string decision = Console.ReadLine(); string x = (decision == "A")? "a" : (decision == "B")? "b" : "wrong letter"; return x; } } public static void Main(string[] args){ TakeDecision dec = new TakeDecision(); if(dec.Choose() == "a"){ Console.WriteLine("a); //--Here's the issue //--When I type "B" in the console it seems like it starts the Choose() method once more //--The second time I write "B" it works. if(dec.Choose() == "b"){ Console.WriteLine("b"); } } }

23rd Apr 2021, 11:32 AM
Soreanu Stefan Alexandru
Soreanu Stefan Alexandru - avatar
2 Antworten
+ 4
// If statement has to be clear made TakeDecision dec = new TakeDecision(); string si = dec.Choose(); if(si == "a"){ Console.WriteLine("a"); }
23rd Apr 2021, 12:16 PM
JaScript
JaScript - avatar
+ 2
Ohh thanks, I'm going to try it soon. I didn't thought that by calling the method directly it would cause this issue. I hope this solved my problem ;D. Tried it.. it doesn't work. It goes directly after the if statement..
23rd Apr 2021, 2:55 PM
Soreanu Stefan Alexandru
Soreanu Stefan Alexandru - avatar