0

こんにちは、次のコードがあります。

static void CalcWordchange()
{
    List<string[]> l = new List<string[]>
        {
        new string[]{Question1, matcheditalian1},
        new string[]{"Sam", matcheditalian2},
        new string[]{"clozapine", matcheditalian3},
        new string[]{"flomax", matcheditalian4},
        new string[]{"toradol", matcheditalian5},
        };

    foreach (string[] a in l)
    {
        int cost = LevenshteinDistance.Compute(a[0], a[1]);
        errorString = String.Format("To change your input: \n {0} \n into the correct word: \n {1} \n you need to make: \n {2} changes \n ".Replace("\n",      Environment.NewLine),
            a[0],
            a[1],
            cost);
    }
}

ボタンがクリックされるたびに、foreach ループ内のテキストが実行され、1 つの文 (リストの最後の項目) が出力されます。私がしたいのは、5つのアイテムすべてを文字列に出力することです。

4 つの新しい変数 (errorString2、3 など) を追加しましたが、それを出力する方法がわかりません。

どんな助けでも大歓迎です、ありがとう

4

3 に答える 3

5

StringBuilderオブジェクトを使用してすべてのパーツを収集してみてください。

StringBuilder buildString = new StringBuilder();
foreach (string[] a in l)
{
    int cost = LevenshteinDistance.Compute(a[0], a[1]);
    buildString.AppendFormat("To change your input: \n {0} \n into the correct word: \n {1} \n you need to make: \n {2} changes \n ".Replace("\n",      Environment.NewLine),
        a[0],
        a[1],
        cost);
}
errorString = buildString.ToString();
于 2013-03-22T19:03:50.470 に答える
2

代わりに、次のようにします。

 string finalOuput = string.empty;
 foreach (string[] a in l)
 {
  int cost = levelshteinDstance.Compute(a[0], a[1]);
  finalOutput += string.Format("To change your input: \n {0} \n into the correct word: \n {1} \n you need to make: \n {2} changes \n ".Replace("\n",      Environment.NewLine),
            a[0],
            a[1],
            cost);
    }
}

// ここに finalOutput を表示

于 2013-03-22T19:01:50.967 に答える
1

List<string>出力を保持する を作成します。

var OutputList = new List<string>();
foreach (string[] a in l)
{
    errorString = ...
    OutputList.Add(errorString);
}

// output
foreach (var s in OutputList)
{
    Console.WriteLine(s);
}

または、次を使用できますStringBuilder

var outputS = new StringBuilder();
foreach (string[] a in l)
{
    errorstring = ...
    outputS.AppendLine(errorString);
}

Console.WriteLine(outputS.ToString());
于 2013-03-22T19:04:16.657 に答える