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");
}
}
}
}
所以我想要做的是檢查是否姓氏是空的這是所有用戶輸入姓氏,但我得到這個錯誤。檢查分割方法是否爲空
未處理的異常:System.IndexOutOfRangeException:索引超出了數組的範圍。 在switchStatementExercise.Program.Main(字串[] args)在C:\用戶\喬\桌面\ cSharpWeek1 \ switchStatementExercise \ switchStatementExercise \的Program.cs:行21
任何幫助或重定向到的答案是大!
爲什麼'fullNameJoe.Split(新[] { ''}'和'不fullNameJoe.Split(新[] {」「}'誰寫的'最大; Gridley' –
我試過這個,但仍然不起作用): –
它只能根據你輸入的內容來工作,你可以用任何字符來分割'fullNameJoe.Split(「,; - /」。ToCharArray )' –