C#拼图中的拼图,无法弄清楚

时间:2016-01-24 18:27:28

标签: c# function for-loop puzzle

This is the whole puzzle

我需要帮助解决这个C#难题:填补空白游戏,答案可以是7个空格中的以下之一

空白看起来像这样_并突出显示&其中有7个

谜题的下一部分是 我可以调用什么C#函数来替换for循环并插入以满足下面代码中的'result ==?':

    var answer = "Please help me with this puzzle please"; 

    var words = new[] { "Please", "help", "me", "with", "this", "puzzle", "please" }; 

    var result = ?

    if ( result == answer )
    Console.WriteLine(“Correct!”);

我猜这个你需要一些linq查询,它只适合,但是如何?

由于我不是C#的粉丝,我个人心里都在吹嘘。

2 个答案:

答案 0 :(得分:1)

解决。基本上只是尝试一些事情,其他一些事情是非常合乎逻辑的(例如由于while(wordCount < 4)递增而导致的wordCount)。其他的东西只是模数运算的东西,使挑战更难一点。 (与x % 1一样,始终为true)。

using System;

class Programm
{
    static void Main()
    {
        var answer = "Please help me with this puzzle please"; 
        var words = new[] { "Please", "help", "me", "with", "this", "puzzle", "please" };

        var result = "";
        var wordCount = 0;

        for (var iCount = 12; iCount > 0; iCount--)
        {
            while (wordCount < 4) //less than because word count get's incremented
            {
                if (iCount % 1 == 0)
                {
                    result += words[wordCount]; 
                    result += " ";
                    wordCount++;
                }

                if ((iCount * 6) == 24)
                {
                    result += words[wordCount]; 
                    result += " ";
                    wordCount++;
                }
                iCount--;
            }

            if (iCount % 3 != 1)
                continue;

            result += words[wordCount]; 

            if (wordCount != 6)
                result += " ";

            wordCount += 1;
        }

        Console.WriteLine("Result: " + result);

        if ( result == answer )
            Console.WriteLine("Correct!");
        else
            Console.WriteLine("FAIL!");
    }
}

答案 1 :(得分:0)

以下是问题的最后一部分:使用string.Join

    var answer = "Please help me with this puzzle please";

    var words = new[] { "Please", "help", "me", "with", "this", "puzzle", "please" };

    var result = string.Join(" ", words);

    if (result == answer)
        Console.WriteLine("Correct!");