我有一個關于在c#編程中使用linq查詢串列的小程式是類
public class Student
{
public string studentname { get; set; }
public int[] CardNumber { get; set; }
}
并創建靜態串列
public static IList<Student> StudentConfig = new List<Student>
{
new() { studentname="Peter", CardNumber= new int[] {2 } },
new() { studentname= "Winnie" , CardNumber= new int[] {3} },
new() { studentname = "Gilbert", CardNumber = new int[] { 2,3} }
};
挑戰是寫一個簡單的函式,基于簡單的配置來找出哪個學生姓名,元素卡號可以乘以輸入值函式如下
public static string MultiplyListCheckUp (int InputValue)
{
// For example InputValue : 4 , then return studentname="Peter" , as 4 is only factor of 2
// For example InputValue : 9 , then return studentname="Winnie" , as 9 is only factor of 3
// For example InputValue : 6 , then return studentname="Gilbert", as 6 is factor of 2 and 3
return string.Empty;
}
關于studentconfig 可以調整。例如我們可以添加新記錄
studentname = "Sally", CardNumber = new int[] { 2,5}
如果輸入值:10,它會回傳 sally 但是,如果輸入值:30 沖突超過 1 個規則,它會回傳“多條記錄發生”或拋出例外
任何人都可以解決它嗎?
謝謝
uj5u.com熱心網友回復:
我不確定您是否正確描述了問題,但條件是:
// For example:
// if inputValue 4, then return "Peter"
// if inputValue 9, then return "Winnie"
// if inputValue 6, then return "Gilbert"
// if inputValue 10, then return "Sally"
// otherwise - show error or throw exception
這個例子應該有效:
public class Student
{
public string Name { get; set; }
public int[] CardNumber { get; set; }
}
class Program
{
public static IList<Student> StudentConfig = new List<Student>
{
new() { Name="Peter", CardNumber = new int[] { 2 } },
new() { Name= "Winnie" , CardNumber = new int[] { 3 } },
new() { Name = "Gilbert", CardNumber = new int[] { 2, 3 } },
new() { Name = "Sally", CardNumber = new int[] { 2, 5 } },
new() { Name = "Bob", CardNumber = new int[] { 3, 6, 8 } },
new() { Name = "Jack", CardNumber = new int[] { 5, 7, 12 } }
};
static void Main()
{
Console.WriteLine("- Students by CardNumber selector -\n");
while (true)
{
Console.Write("Your input: ");
if (!int.TryParse(Console.ReadLine(), out var inputNumber))
{
Console.Write("Invalid number was inputed. Want to try again? (Y/N): ");
if (Console.ReadLine().ToUpper() != "Y")
break;
else
continue;
}
var foundedStudents = StudentConfig.Where(student => (student.CardNumber.Length > 1
? student.CardNumber.Aggregate(1, (a, b) => a * b)
: student.CardNumber[0] * student.CardNumber[0])
== inputNumber);
var count = foundedStudents.Count();
switch (count)
{
case 1:
Console.WriteLine("Founded Student: " foundedStudents.First().Name);
break;
case 0:
Console.WriteLine("There are no records at result with provided number");
break;
case > 1:
Console.WriteLine("There are multiple records at result with provided number");
break;
default:
Console.WriteLine("How it is possible to get here?");
break;
}
}
Console.WriteLine("\nProgram complete. Press any key to close console...");
Console.ReadKey();
}
}
所以我使用了一個三元,它檢查Student'sCardNumber陣列的長度,如果.Length > 1使用.Aggregate()或乘以本身的值,則乘以每個陣列值。三元結果與用戶輸入進行比較,比較結果.Where()方法過濾StudentConfig集合。
過濾后 - 檢查檢索了多少元素,根據您的條件,如果只有 1 個Student- 列印,如果沒有找到或超過 1 - 顯示錯誤。
while在用戶輸入某些內容(而不是數字)之前,所有這些都包含在回圈中。
結果看起來像這樣:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/325845.html
上一篇:使用方法includes()檢查字串中是否出現陣列中表示的字符回傳true-從字串中洗掉字符
下一篇:python中基于陣列的列舉
