using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace switchStatementExercise
{
class Program
{
static void Main(string[] args)
{
String response;
Console.WriteLine("Please Vote for your president out of the 7 following, Joseph Mason, James Long, Ben Harding, Georgia Mason, Keith Webb, Mark Grimley, Max Gridley");
response = Console.ReadLine();
string fullNameJoe = response;
var names = fullNameJoe.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
string firstName = names[0];
string lastName = names[1];
Console.WriteLine(lastName);
if (response.Equals(fullNameJoe) || response.Equals(firstName) || response.Equals(lastName))
{
Console.WriteLine("You have voted for " + fullNameJoe);
Console.WriteLine(fullNameJoe);
}
else if (fullNameJoe.Length > 1 && lastName == null)
{
Console.WriteLine("You need a last name");
}
else
{
Console.WriteLine("Please enter a first name and last name");
}
}
}
}
Итак, что я пытаюсь сделать, это проверить, является ли последнее имя пустым, это был всего лишь пользователь, чтобы ввести фамилию, но я получаю эту ошибку.Проверка наличия метода Split
Unhandled Exception: System.IndexOutOfRangeException: Индекс находился за пределами массива. в switchStatementExercise.Program.Main (String [] арг) в C: \ Users \ Joe \ Desktop \ cSharpWeek1 \ switchStatementExercise \ switchStatementExercise \ Program.cs: строка 21
Любая помощь или перенаправление в ответ будет Великий!
Почему 'fullNameJoe.Split (новый [] { ';'}. ', А не' fullNameJoe.Split (новый [] {»«} 'Кто будет писать' Max ; Gridley' –
Я пробовал это, и это все еще не работает): –
Он работает только в соответствии с тем, что вы набираете. Вы можете разделить любые символы 'fullNameJoe.Split (",; -/") ToCharArray () ' –