2014-09-14 3 views
0

В моем цикле while он запрашивает у пользователя ввод значения и после ввода значения вводит определенные методы. Проблема, с которой я сталкиваюсь, заключается в том, что пользователю предлагается дважды ввести значение (т. Е. Запросить ввести A, B или X. Пользователь вводит значение, а затем программа снова просит ввести A, B или X, не выполняя правильную операцию). Любая помощь, чтобы она работала после ввода, была бы замечательной!, в то время как повторяющиеся входы пользователя в цикле C#

  class output 
     { 
     static void Main(string[] args) 
     { 
      char userInput; 
      char upper; 

      Accounts myAccounts = new Accounts(); 
      myAccounts.input(); 
      Console.WriteLine("Enter an A to search account numbers"); 
      Console.WriteLine("Enter a B to average the accounts"); 
      Console.WriteLine("Enter X to quit the program"); 
      Console.WriteLine("Enter an option --->"); 

      userInput = Convert.ToChar(Console.ReadLine()); 
      upper = char.ToUpper(userInput); 

      while (upper != 'X') 
      { 

       if (upper == 'A') 
       { 
        myAccounts.search(); 
        Console.WriteLine("Enter an A to search account numbers"); 
        Console.WriteLine("Enter a B to average the accounts"); 
        Console.WriteLine("Enter X to quit the program"); 
        Console.WriteLine("Enter an option --->"); 
        userInput = Convert.ToChar(Console.ReadLine()); 
        upper = char.ToUpper(userInput); 
       } 
       else if (upper == 'B') 
       { 
        myAccounts.average(); 
        Console.WriteLine("Enter an A to search account numbers"); 
        Console.WriteLine("Enter a B to average the accounts"); 
        Console.WriteLine("Enter X to quit the program"); 
        Console.WriteLine("Enter an option --->"); 
        userInput = Convert.ToChar(Console.ReadLine()); 
        upper = char.ToUpper(userInput); 
       } 
       else 
        Console.WriteLine("You entered an incorrect option, please select new option"); 
       Console.WriteLine("Enter an A to search account numbers"); 
       Console.WriteLine("Enter a B to average the accounts"); 
       Console.WriteLine("Enter X to quit the program"); 
       Console.WriteLine("Enter an option --->"); 
       userInput = Convert.ToChar(Console.ReadLine()); 
       upper = char.ToUpper(userInput); 
      }   
     } 
    } 
+0

Srsly .. читайте о методах ... например. 'PrintMenu()' 'EvalInput (ввод char)' и т. Д. –

+0

Что в среднем, методы поиска и ввода? – Tuco

ответ

1

Попробуйте переформатировать ваш главный так:

static void Main(string[] args) 
{ 
    char userInput; 
    char upper; 

    Accounts myAccounts = new Accounts(); 
    myAccounts.input(); 

    do 
    { 

     Console.WriteLine("Enter an A to search account numbers"); 
     Console.WriteLine("Enter a B to average the accounts"); 
     Console.WriteLine("Enter X to quit the program"); 
     Console.WriteLine("Enter an option --->"); 
     userInput = Convert.ToChar(Console.ReadLine()); 
     upper = char.ToUpper(userInput); 

     if (upper == 'A') 
     { 
      myAccounts.search(); 
     } 
     else if (upper == 'B') 
     { 
      myAccounts.average(); 

     } 
     else 
      Console.WriteLine("You entered an incorrect option, please select new option"); 
    } 
    while (upper != 'X')  
} 

его не совершенна, но является хорошей отправной точкой. По сути, вы дважды видите свое приглашение, потому что вы показываете меню один раз перед входом в цикл while, а затем снова после выполнения операции. цикл do while, который я предоставляю, должен исправить вашу проблему.

+1

А также, если поток выполняется, он печатает меню и ** также **, когда поток выходит. Этот вопрос действительно является временем. Если бы он поставил брекеты в другом, он бы понял проблему. На самом деле отсутствие брекетов в другом случае заставляет меня подозревать, что это преднамеренный вопрос (циничный). –

Смежные вопросы