当试图从另一个方法输出乱码时我得到System.Char []

时间:2017-02-09 21:00:05

标签: c# visual-studio-2015

我向我的scramble方法发送一个单词,该方法返回加扰的单词,但当我尝试输出单词时,我得到System.Char []。我见过另一个类似于我的线程,但是他使用了.ToCharArray和.ToString,这是他的问题,我无法从中找出答案。这是C#,我还在学习。我不想要答案。我想知道我做错了什么,以及如何解决它的建议。提前谢谢。(我不知道for循环出现的原因)

namespace Farmer_JumbleApp
{

//Author: Aaron Farmer
class Jumble
{
    string hiddenWord = "";//chosen word from array
    string[] words = new string[] { "sauce", "iphone", "tick", "time", "think", "dream", "awake" };
    Random randy = new Random();

    string display = " ";

    public Jumble()
    { }

    public void Master()
    {

    UIJumble Graphics = new UIJumble();
    Admin Info = new Admin();

        string userGuess = " ";        
        int randNum = 0;
        int quit;
        bool cont = true;//Loop condition
        char[] scrambledWord = new char[] { };//Scrambled word


        randNum = randy.Next(words.Length);
        hiddenWord = words[randNum];
        scrambledWord = Scramble(hiddenWord);

        while (cont)
        {
            display = "The Scrambled Word is: ";
            Graphics.DisplayOnLine(display);

            for (int i = 0; i < scrambledWord.Length; i++)
            {
                display = "" + scrambledWord;
                Graphics.DisplayOnLine(display);
            }

            //display = "  ";
            //Graphics.DisplayString(display);

            display = "\n\n\nEnter your guess: ";
            Graphics.DisplayOnLine(display);

            userGuess = Graphics.RecieveString();

            if (userGuess == hiddenWord)
            {
                Graphics.CleanUp();
                Info.MyInfo();
                display = "You are correct";
                Graphics.DisplayString(display);

            }//end of if


            else if (userGuess != hiddenWord)
            {
                display = "Sorry, You are Incorrect";
                Graphics.DisplayString(display);
            }//end of else if


            display = "Would you like to quit?\n1. Yes\n2. No";
            Graphics.DisplayString(display);
            quit = Graphics.RecieveInt();

            if (quit == 1)
            {
                cont = false;   
            }



        }//End of While_Loop
        display = "Hope you enjoyed";
        Graphics.DisplayString(display);
        Environment.Exit(0);
    }//End of Master Method

-----------------------------下一个方法---------------- ----

    public char[] Scramble(string word)
    {

        int randNum = 0;
        int random;
        char temp;



        randNum = randy.Next(words.Length);
        hiddenWord = words[randNum];

        var displayWord = new char[word.Length]; //initializing array
        for (int i = 0; i < word.Length; i++)
            {
                do
                    random = randy.Next(word.Length);
                while (displayWord[random] != 0);
                temp = word[i];
                displayWord[random] = temp;
            }


        return displayWord;
    }//End of Scramble Method


}//End of Class
}//End of Namespace

1 个答案:

答案 0 :(得分:2)

for (int i = 0; i < scrambledWord.Length; i++)
{
    display = "" + scrambledWord;
    Graphics.DisplayOnLine(display);
}

应替换为

for (int i = 0; i < scrambledWord.Length; i++)
{
    display = "" + scrambledWord[i];
    Graphics.DisplayOnLine(display);
}

因为"" + scrambledWord将使用scrambledWord.ToString()返回System.Char[](其类型名称)

很快就提出了,Joel Coehoorn建议:

Graphics.DisplayOnLine(new String(scrambledWord));