【问题标题】:C# For Loop How to Display Only Matching ValuesC# For 循环如何只显示匹配的值
【发布时间】:2021-06-16 05:41:47
【问题描述】:

我的任务是创建一个至少有两个匹配值的字符串列表,然后提示用户输入其中一个值,然后我要显示输入的那个值的一个或多个索引。

我似乎已经知道如何显示所需的索引,但如果输入的值不在列表中,并且当我执行 else 时,它​​会搞砸,我还需要声明。在这方面寻求帮助!

所以这就是我想要的,但没有 else 语句:

using System;
using System.Collections.Generic;


class Program
{
    static void Main(string[] args)
    {

        List<string> mprovinces = new List<string> { "Nova Scotia", "New Brunswick", "Prince Edward Island", "Nova Scotia" };
    Console.WriteLine("Enter one of the following maritime provinces: \nNova Scotia, \nNew Brunswick, \nPrince Edward Island\n");
    string input2 = Console.ReadLine();


    for (int i = 0; i < mprovinces.Count; i++)
    {
        bool match = mprovinces[i] == input2;

        if (match)
        {
            Console.WriteLine(i);
        }
    }
}
}

但是当我添加一个 else 语句并输入一个列表中的值时,它提供的数据比我想要的要多:

    using System;
    using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {

        List<string> mprovinces = new List<string> { "Nova Scotia", "New Brunswick", "Prince Edward Island", "Nova Scotia" };
    Console.WriteLine("Enter one of the following maritime provinces: \nNova Scotia, \nNew Brunswick, \nPrince Edward Island\n");
    string input2 = Console.ReadLine();


    for (int i = 0; i < mprovinces.Count; i++)
    {
        bool match = mprovinces[i] == input2;

        if (match)
        {
            Console.WriteLine(i);
        }
        else
        {
            Console.WriteLine("Sorry, this is not in the list");
        }
    }
    }
    }

【问题讨论】:

  • 数据多于您的需要是什么意思?如果要在执行 else 语句时结束循环,请使用 break;走出循环
  • 这是你开始学习编程的最佳时机——学习how to step through code in the debugger。
  • 对于提示,我可以建议Console.WriteLine("Enter one of the following maritime provinces:\n" + String.Join("\n", mprovinces));
  • 疯狂反思是对的!我明白了,所以我尝试了一些“else if”并试图说 (!match) 但这也不起作用,因为它每次都在经历。那么我会以某种方式将该语句放在我的循环之外吗?或者我应该使用不同的循环,比如 do/while?
  • 感谢您的提示,乔尔。我已经调整了提示!

标签: c# list loops for-loop if-statement


【解决方案1】:

由于“抱歉”消息在 for 循环内,因此您将为每个与您的 input2 不完全匹配的索引显示它。您需要将匹配值拉出 for 循环并检查一次。

var matchIndex = -1; // can never get a -1 so it's a good default to check for later

for (int i = 0; i < mprovinces.Count; i++)
{
    if (mprovinces[i] == input2)
    {
        matchIndex = i;
        break; // break out of loop
    }
}

if (matchIndex >= 0) // again a valid index will never be below 0
{
    Console.WriteLine("Index: " + matchIndex);
}
else
{
    Console.WriteLine("Sorry, this is not in the list");
}

我不确定你想展示什么。如果您只想显示匹配的第一个索引,那么上面的代码将起作用。如果您想显示每个匹配的索引,那么这将起作用。

编辑:我已将此函数更改为不使用字符串来跟踪列表。它只是显示匹配的索引。

bool matchExists = false;

for (int i = 0; i < mprovinces.Count; i++)
{
    if (mprovinces[i] == input2)
    {
        matchExists = true;
        Console.WriteLine(i);
    }
}

if (matchExists == false) 
{
    Console.WriteLine("Sorry, this is not in the list");
}

【讨论】:

  • 不要使用字符串来存储列表。使用实际的列表类型,例如List&lt;int&gt;。
  • 通常我会同意,但由于这看起来像是一个输出非常简单的初学者代码,我想不要在混合中添加太多额外的代码。不确定他们是否会被引入列表。
  • 初学者不应该指向这个方向。列表很简单。很多时候,这会导致字符串连接地狱,因为他们认为这“没问题”。
  • 考虑到这个问题在Main 的第一条语句中声明了一个List&lt;string&gt; 变量,我假设他们至少得到了一些提及。
  • 是的!您的编辑是我试图做的,但是因为我们只被教导使用带有 while 的 bool 而我不知道如何使它与 for 循环一起工作而感到困惑。非常感谢大家的反馈和建议!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
相关资源
最近更新 更多