2016-10-24 10 views
-1

Windowsコンソールアプリケーションを使用して文中の単語数を計算しようとしています。しかし、私は、配列のものに新たなんだと本当に私はそのコードで何ができるか分からない:(単語が==空間の間のいずれかの文字が)あなたが何かを実装することができ最も簡単な場合System.Arrayには長さの定義が含まれていません

string sentence; 
Console.WriteLine("Enter your sentence:"); 
sentence = Console.ReadLine(); 

string [] words = sentence.Split(' '); 
Console.WriteLine(sentence.Lenght); // .Lenght is where I get the error from 
Console.ReadKey(); 
+2

あなたは間違って綴りましたが、それは 'Length'です。 – Equalsk

+2

長さは入力時に綴られていますが、長さではありません:) –

+2

文字列に 'Length'を呼び出します:' sentence'は配列にありません: 'words'。それは 'Console.WriteLine(words.Length);' – croxy

答えて

1

私は、任意のスペルミスがあった場合、私はhere.Now投稿する前に、あなたは正しく長さワードを綴るとき、それはみんなworks.Sorry、しかし、おかげで確認していないと本当にばか

Console.WriteLine("Enter your sentence:"); 

// try not declaring local variable prematurely, but exacly where you want them 
string sentence = Console.ReadLine(); 

// StringSplitOptions.RemoveEmptyEntries - what if user starts with space? 
// separate words with two spaces, e.g. " This is a test input " 
// we want 5, right? 
int count = sentence 
    .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) 
    .Length; // Please, notice spelling 

Console.WriteLine(count); 

Console.ReadKey(); 
0

イム:このように。

関連する問題