Compilation error



  • namespace SwitchFor
    {
    class Program
    {
        static void Main(string[] args)
        {
            int i = 3; int j = 5; uint s = 0; char n;
            for (int a = 0; a < 5; a++ )
            {
                Console.WriteLine("Введите метку 1, 2, 3, 4, 5");
                n = (char)Console.Read();
                switch (n)
                {
                    case '1':
                        {
                            j = j - i;
                            s = s + (uint)j;
                        }
                        break;
                    case '2':
                        {
                            j = j + i * 3;
                            s = s + (uint)j;
                        }
                        break;
                    case '3':
                        {
                            j = j--;
                            s = s + (uint)j;
                        }
                        break;
                    case '4':
                        {
                            j = (i - 5) * 2;
                            s = s + (uint)j;
                        }
                        break;
                    case '5':
                        {
                            j = i + j + 1;
                            s = s + (uint)j;
                        }
                        break;
                    default:
                        {
                            s = s + 1;
                        }
                        break;
                }
            }
            Console.WriteLine("Сумма равна "+s);
            Console.ReadKey();
        }
    }
    }
    

    It's such a small program, but it's not working right.

    Вывод должен быть таким:
    

    Введите метку 1, 2, 3, 4, 5
    1
    Введите метку 1, 2, 3, 4, 5
    2
    Введите метку 1, 2, 3, 4, 5
    3
    Введите метку 1, 2, 3, 4, 5
    4
    Введите метку 1, 2, 3, 4, 5
    5
    Сумма равна (и какое-то число, полученное в результате этих пяти действий).



  • Replace, as you lined up,

    n = (char)Console.Read();
    

    On:

    n = Console.ReadLine()[0];
    

    And your cycle will work correctly.

    quote https://msdn.microsoft.com/ru-ru/library/system.console.read(v=vs.90).aspx :

    Return of control by method Readblocked before input symbols are introduced from keyboard; method after application by user claws Enter♪ In response to the compression claws Enter entry data Added by the platform the sequence of symbols, identifiable end of line (e.g. Windows added the sequence of symbols "return "Translation lines." Subsequent challenges of the method ReadUser input One symbol at a time. After Removing the last symbol method Read Re return blocked again. control and cycle repeat.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2