Console.WriteLine("Here you can write the name as many as you want,
and if u wanna end up just write No!");
Console.WriteLine("\n");
Console.WriteLine("Start writing a name:");
string[] namnArray = new string[200];
for (int i = 0; i < namnArray.Length; i )
{
Console.ForegroundColor = ConsoleColor.Yellow;
namnArray[i] = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Do u wanna write another one? ");
if (namnArray[i] == "No")
{
Console.WriteLine("\n");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("write a name of your choice:");
//Here i wanna know how many times a names used as the user wrote up there!
but dont know how to do it, if you wanna help me will be thankful!
}
}
我試圖將一個新的字串變數設為
string youChoice = Console.ReadLine();
然后我不知道如何更進一步!
uj5u.com熱心網友回復:
我不會為此使用固定大小的陣列。你幾乎肯定會更好地使用它List<string>
。
var namnList = new List<string>();
string enteredNamn;
do {
...
enteredNamn = Console.ReadLine();
namnList.Add(enteredNamn);
} while (enteredNamn != "No");
然后您可以使用List<>.Count()
來計算所有匹配項。
var chosenNamn = Console.ReadLine();
var count = namnList.Count(x => x == chosenNamn);
免責宣告 - 僅用于說明目的的代碼,我不保證這甚至可以編譯
uj5u.com熱心網友回復:
Console.WriteLine("在這里你可以寫多少你想寫的名字,如果你想結束就寫No!");
Console.WriteLine("\n");
Console.WriteLine("Start writing a name:");
string[] namnArray = new string[200];
for (int i = 0; i < namnArray.Length; i )
{
Console.ForegroundColor = ConsoleColor.Yellow;
namnArray[i] = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Do u wanna write another one? ");
if (namnArray[i] == "No")
{
Console.WriteLine("\n");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("write a name of your choice:");
var names = string.Join("", namnArray);
var input = Console.ReadLine();
var count = Regex.Matches(names, input).Count;
Console.WriteLine("The number of ocurance is " count);
}
uj5u.com熱心網友回復:
克諾比將軍。你是一個大膽的人。
您可以使用Array.FindAll:
var number = Array.FindAll(namnArray, x => x == name ).Count();
FindAll
檢索與指定謂詞定義的條件匹配的所有元素。
有關 FindAll 的更多資訊和使用示例,請點擊官方檔案的鏈接。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/526730.html
標籤:C#