C# 列表复杂性
我完全理解了这些列表,但在我陷入困境之前,我还有一件事需要知道C# 列表复杂性,c#,visual-studio,list,declare,C#,Visual Studio,List,Declare,我完全理解了这些列表,但在我陷入困境之前,我还有一件事需要知道 我创建了一个带字符串的列表和一个带int的列表 第一个字符串名为jack,其值为2。第二个字符串 名字是john,它的值是5,但结果是我 约翰的价值2约翰的价值5 杰克的价值2杰克的价值5 如何使第一个字符串只有一个值,而第二个字符串有自己的值 List<string> NameList = new List<string>(); Console.WriteLine("Please inser
我创建了一个带字符串的列表和一个带int的列表 第一个字符串名为jack,其值为2。第二个字符串 名字是john,它的值是5,但结果是我 约翰的价值2约翰的价值5
杰克的价值2杰克的价值5 如何使第一个字符串只有一个值,而第二个字符串有自己的值
List<string> NameList = new List<string>();
Console.WriteLine("Please insert a name ");
string name = Console.ReadLine();
NameList.Add(name);
List<int> answerList = new List<int>();
Console.WriteLine("Please insert a value");
int x = int.Parse(Console.ReadLine());
answerList.Add(x);
Console.WriteLine("Do you want to calculate more ? (yes/no)");
string answer = Console.ReadLine();
answer.Trim();
bool isYes = true;
while (isYes)
{
if (answer == "yes")
{
Console.WriteLine("Please insert another name/value");
name = Console.ReadLine();
x = int.Parse(Console.ReadLine());
NameList.Add(name);
answerList.Add(x);
Console.WriteLine("Do you want to calculate more ? (yes/no)");
answer = Console.ReadLine();
answer.Trim();
}
else if (answer == "no")
{ break; }
} string are = " are ";
foreach (var NAME in NameList)
foreach (var item in answerList)
Console.WriteLine("The values of " + NAME + are +item);
List NameList=新列表();
Console.WriteLine(“请插入名称”);
字符串名称=Console.ReadLine();
姓名列表。添加(姓名);
List answerList=新列表();
Console.WriteLine(“请插入值”);
intx=int.Parse(Console.ReadLine());
答复清单。添加(x);
Console.WriteLine(“您想计算更多吗?(是/否)”);
字符串answer=Console.ReadLine();
回答:Trim();
bool isYes=true;
while(是的)
{
如果(回答=“是”)
{
Console.WriteLine(“请插入其他名称/值”);
name=Console.ReadLine();
x=int.Parse(Console.ReadLine());
姓名列表。添加(姓名);
答复清单。添加(x);
Console.WriteLine(“您想计算更多吗?(是/否)”);
answer=Console.ReadLine();
回答:Trim();
}
否则如果(回答=“否”)
{break;}
}字符串are=“are”;
foreach(名称列表中的变量名称)
foreach(应答列表中的var项)
Console.WriteLine(“+NAME+are+item”的值);
问题在于:
foreach (var NAME in NameList)
foreach (var item in answerList)
您正在进行交叉联接,将名称列表
中的每个名称与应答列表
中的每个值进行匹配。您有几个选择:
- 创建一个具有
和Name
属性的类,并将该类的实例存储在单个列表中Value
- 使用Linq函数逐个元素排列列表,并在
foreach
- 使用一致的标准大写字母。变量使用camelCase(例如
,名称列表
),属性使用PascalCase(应答列表
,名称
)。对常量值使用所有大写应答
- 请记住,
返回经过修剪的字符串,因此Trim
什么也不做(它会丢弃answer.Trim()
的输出)。改用Trim
answer=answer.Trim()
.Zip
只会是一种更好的方法,如果你加入了来自不同来源的无法组合的列表。谢谢你所做的一切,但还有一件事告诉我在google上搜索类的正确方法,因为我看到了更多关于它们的内容,并试图与它们为伍,但我几乎不使用它们。我会搜索C#class
I我真的无法做到这一点,我创建了一个类,但结果我的第二个名字覆盖了第一个名字,没有foreach我真的很笨