我正在做一個任務,顯示2004年有多少國家加入歐盟。 我還希望收到2004年加入的國家的名稱。我開始做我的作業,但我只得到塞浦路斯。 麻煩的是,有幾個國家在2004年加入。 如何獲得所有在2004年加入的國家的名字? 我在控制臺應用程式(.NET框架)中完成了我的任務。 對于這項任務,我使用了一個包含資料的txt檔案。 以下是我的代碼:
class Program<
{
static List<Country> joining = new List< Country>()。
static void read()
{
string[] rows=File.ReadAllLines("EU.txt"/span>)。
foreach (var item in rows)
{
joining.Add(new Country(item))。
}
}
static void Main(string[] args)?
{
read()。
//多少個國家:
Console.WriteLine($"{joining.Count(item =>item.date.Split(' . ')[1] == "05" )}countries joined.") 。
//names:.
Console.WriteLine($"Country names:{joining.Find(item =>item.date.Split(' . ')[1] == "05" ).name}"); // !!! 這個代碼很糟糕。
Console.ReadKey()。
}
}
class Country
{
//Fields
public string date, name;
//ctor
public Country(string row)
{
name=row.Split(';')[0] 。
date = row.Split('; ')[1]。
}
EU.txt:
Austria;1995.01.01 比利時;1958.01.01 保加利亞;2007.01.01 塞浦路斯;2004.05.01 捷克;2004.05.01 丹麥;1973.01.01 聯合王國;1973.01.01 愛沙尼亞;2004.05.01 芬蘭;1995.01.01 法國;1958.01.01 希臘;1981.01.01 Netherlands;1958.01.01 克羅地亞;2013;07.01 愛爾蘭;1973.01.01 波蘭;2004.05.01 拉脫維亞;2004.05.01 立陶宛;2004.05.01 盧森堡;1958.01.01 匈牙利;2004.05.01 馬耳他;2004.05.01 德國;1958.01.01 意大利;1958.01.01 葡萄牙;1986.01.01 羅馬尼亞;2007.01.01 西班牙;1986.01.01 瑞典;1995.01.01 斯洛伐克;2004.05.01 Szlovenia;2004.05.01
uj5u.com熱心網友回復:
好吧,joining.Find(...)將只找到第一個項(在你的例子中是Cyprus)。你可以使用Linq Where代替。請注意,你應該與"2004"(年)進行比較,而不是與"05"(月)進行比較:
using System.Linq;
...
// names:
var names = joining
.Where(item => item.date.Split('。')[0] == "2004")
.Select(item => item.name)。
// let's join all the names by " , " :
Console.WriteLine($"Country names:{string.Join(", ", names)})。
編輯:然而,像item.date.Split('.')[1] =="2004"這樣的比較看起來很難看(為什么我在查詢dates時要考慮到字串?
讓Country類來幫助自己(日期、名字等):
class Country {
public string Name {get; }
public DateTime JoinedDate {get;}.
public Country(string row) {
if (row == null)
throw new ArgumentNullException(nameof(row))。
string[] items = row.Split('; ', 2);
if (items.Length < 2)
throw new FormatException("無效的行格式")。
Name = items[0]。
JoinedDate = DateTime.ParseExact(items[1], "yyyy.m.d")。
}
public override string ToString() => 名稱。
}
然后是Linq(我們查詢EU.txt檔案):
static void Main(string[] args) {
List<Country> joined2004 = File
.ReadLines("EU.txt")
.Select(line => new Country(line))
.Where(country => country.JoinDate.Year == 2004)
.ToList()。
Console.WriteLine($"{joined2004.Count}國家加入。")。
Console.WriteLine($"{string.Join(", ", joined2004)})
uj5u.com熱心網友回復:
這里有很多小的變化:
class Program
{
static List<Country> Countries;
//使用型別和方法,避免一次性將全部集合加載到RAM中。
//盡可能長的時間。這意味著IEnumerable而不是List和ReadLines()。
//而不是ReadAllLines()。
//對于read()函式來說,接受一個值并回傳結果也更好。
static IEnumerable<Country> ReadCountries(>string filePath)
{
//沒有必要通過ReadAllLines()分配那么多的RAM。
//更好的做法是每次只在RAM中存放一行,直到所有內容都加載完畢。
return File.ReadLines(filePath)
.Select(line => new Country(line))。
}
static void Main(string[] args)?
{
Countries = ReadCountries("EU.txt").ToList()。
var JoinedIn2004 = Countries.Where(c => c.date.Year == 2004) 。
Console.WriteLine($"{JoinedIn2004.Count()}國家加入。
國家名稱:")。)
//需要回圈瀏覽串列以獲得所有的名字。
foreach(var country in JoinedIn2004)
{
Console.WriteLine(country.name)。
}
//或者,如果你真的不想寫一個回圈:
// Console.WriteLine(string.Join("
", JoinedIn2004));
Console.ReadKey(true)。
}
}
class Country; }
{
//屬性 > 欄位
public string name {get; set; }
public DateTime date {get; set; } //Parse an actual DateTime for this!
//個人傾向于使用靜態構建器方法(Country.FromTextLine())
//為此,將構建型別與任何特定的檔案相分離,
///但我也知道這是個大問題。
public Country(string row)
{
var format = "yyyy.MM.dd"。
var fields = row.Split('; ');
name = fields[0]。
date = DateTime.ParseExact(fields[1], format, null)。
}
}
uj5u.com熱心網友回復:
List<Country> result = joining.FindAll(item =>item.date.Split('。')[1] =="05") 。
Console.Write($"國名:")。
foreach(Country country in result)
{
Console.Write($"{country.name}"/span>)。
}
Console.WriteLine()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/322813.html
標籤:
上一篇:在f#中使用自定義函式相交
