[英]my Distinct function is not working and i can't see why c#
Distinct function 在應有的情況下無法正常工作。 這是我正在使用的輸入:
one one two
Distinct
End
這是我的全部代碼:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Array.Processing
{
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine();
List<string> texts = input.Split(" ").ToList();
string text = Console.ReadLine();
int a = 0;
while (text != "END")
{
text = Console.ReadLine();
List<string> infos = text.Split(" ").ToList();
if (text == "Distinct")
{
texts = texts.Distinct().ToList();
}
if (text == "Reverse")
{
texts.Reverse();
}
if (infos[0] == "Replace")
{
if (texts.Count > int.Parse(infos[1]) && int.Parse(infos[1]) >= 0)
{
texts.RemoveAt(int.Parse(infos[1]));
texts.Insert(int.Parse(infos[1]), infos[2]);
}
else
{
a++;
}
}
}
for(int n = 0; n < a; n++)
{
Console.WriteLine("Invalid input!");
}
foreach (string info in texts)
{
Console.Write(info + " ");
}
}
}
}
這是我收到的 output:
one one two
我無法弄清楚為什么兩個“一個”仍然存在。 到目前為止已經查看了一個多小時的代碼,但仍然沒有...
首先你有
string input = Console.ReadLine(); // one one two
接下來你有
string text = Console.ReadLine(); // Distinct
接下來,第一次while
你有的時候
text = Console.ReadLine(); // End
此時,您檢查if (text == "Distinct")
但現在它已被覆蓋為“End”,因此您永遠不會最終在列表中調用Distinct()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.