2017-01-09 3 views
2

Я новичок в кодировании и написал эту небольшую программу чтения консоли, чтобы попытаться понять массивы, методы и т. Д.
Я знаю, что мой код очень ошибочен и, вероятно, неправильно написан (по книге).
Мне нужна помощь в том, как исправить мою программу, но проблема связана с тем, что консоль позволяет мне вводить число, но оно не читается из моего метода выбора. Любая помощь будет оценена в значительной степени.Изучение C#, написала ошибочную программу, нужна помощь относительно того, почему она не работает, как я хочу ее

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace PalacioGameImproved 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     WelcomeMessage(); 
     choices(); 

    } 


    public static string WelcomeMessage() 
    { 
     string writeLines; 
     Console.WriteLine("Pick a DooWop"); 
     Console.WriteLine("{0,15}", "0 = Johnny"); 
     Console.WriteLine("{0,13}", "1 = Nick"); 
     Console.WriteLine("{0,15}", "2 = Conrad"); 
     Console.WriteLine("{0,14}", "3 = Diane"); 
     Console.WriteLine("{0,13}", "4 = Rick"); 
     writeLines = Console.ReadLine(); 
     return writeLines; 
    } 

    public static void choices() 
    { 


     string[] names = new string[5]; 
     names[0] = "Johnny"; 
     names[1] = "Nick"; 
     names[2] = "Conrad"; 
     names[3] = "Diane"; 
     names[4] = "Rick"; 

     string UserInput = Console.ReadLine(); 

     if (UserInput == "0") 
     { 
      Console.WriteLine("is it the array"); 
     } 

     else if (UserInput == "1") 
     { 
      Console.WriteLine(names[1]); 
     } 

     else if (UserInput == "2") 
     { 
      Console.WriteLine(names[2]); 
     } 

     else if (UserInput == "3") 
     { 
      Console.WriteLine(names[3]); 
     } 

     else if (UserInput == "4") 
     { 
      Console.WriteLine(names[4]); 
     } 
     else 
     { 
      Console.WriteLine("That was not one of the choices, please try again."); 
      WelcomeMessage(); 
     } 
    } 
} 

}

+0

Что вы имеете в виду, не читает из моего метода выбора? Выбор() кажется мне правильным. Что такое 'writeLines'? –

+0

В writeLines было установлено значение для возврата строки из Console.ReadLine, потому что без нее я получил ошибку о не всех путях кода, возвращающих значение. – JohnDevince

+0

Пожалуйста, не стесняйтесь опубликовать сообщение, вы можете попросить SE удалить пользователя из этого вопроса, другие пользователи потратили время, чтобы ответить на него. –

ответ

3

используется Console.ReadLine 2 раза. .net Fiddle example Ваш код должен быть

using System; 

public class Program 
{ 
    public static void Main() 
    { 
     WelcomeMessage(); 
     choices(); 
    } 

    public static void WelcomeMessage() 
    { 
     Console.WriteLine("Pick a DooWop"); 
     Console.WriteLine("{0,15}", "0 = Johnny"); 
     Console.WriteLine("{0,13}", "1 = Nick"); 
     Console.WriteLine("{0,15}", "2 = Conrad"); 
     Console.WriteLine("{0,14}", "3 = Diane"); 
     Console.WriteLine("{0,13}", "4 = Rick"); 
    } 

    public static void choices() 
    { 
     string[] names = new string[5]; 
     names[0] = "Johnny"; 
     names[1] = "Nick"; 
     names[2] = "Conrad"; 
     names[3] = "Diane"; 
     names[4] = "Rick"; 

     string UserInput = Console.ReadLine(); 

     if (UserInput == "0") 
     { 
     Console.WriteLine("is it the array"); 
     } 

     else if (UserInput == "1") 
     { 
     Console.WriteLine(names[1]); 
     } 

     else if (UserInput == "2") 
     { 
     Console.WriteLine(names[2]); 
     } 

     else if (UserInput == "3") 
     { 
     Console.WriteLine(names[3]); 
     } 

     else if (UserInput == "4") 
     { 
      Console.WriteLine(names[4]); 
     } 
     else 
     { 
      Console.WriteLine("That was not one of the choices, please try again."); 
      WelcomeMessage(); 
     } 
    } 
} 
+0

Спасибо вам большое! Есть ли какие-либо данные в моем коде? Все, о чем мне нужно быть осторожным в будущем? – JohnDevince

+0

Проверьте сайт Exchange Review Stack Exchange. – BLT

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