Bahey Saied Bahey Saied - 27 days ago 14
C# Question

C# Console Create simple app not longer than 2 lines

Hello Guys i am new here so hope to have fun together my teacher asked me to do a simple app just for that ( when the user enter number "1" it writes "2" when enter "2" it writes "1" ) i am with you that is to easy for using if or switch case but she told me to not to exceed two lines

please anybody help i am sucked

Answer

One way to do is use a ternary operator inside another ternary operator. It does the job in two lines.

    var input = Console.ReadLine();
    Console.WriteLine((input == "1") ? ("2") : (input == "2" ? "1" : "Enter 1 or 2"));