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:インデックスが配列の境界外にありました。 \ Users \ユーザージョー\デスクトップ\ cSharpWeek1 \ switchStatementExercise \ switchStatementExercise \ Program.csを::21行
答えに任意のヘルプやリダイレクトがだろうCでswitchStatementExercise.Program.Main(文字列[] args)をで すばらしいです!
なぜ 'fullNameJoe.Split(新しい[]を{ ''}チェック名に直接行くことができます; Gridley' –
私はこれを試してもまだ動作しません): –
これは、あなたが入力しているものに従って動作します。 'fullNameJoe.Split("、; -/"ToCharArray ) ' –