CS1513}预期

时间:2018-02-17 21:31:40

标签: c#

我收到了CS1513}预期的错误消息,我知道这意味着什么,但我无法找到它的来源。我现在一直盯着屏幕看15分钟,试图找出错误。有人可以帮助我吗?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Machine_Learning
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get the amount of letters
            Console.WriteLine("How many letters would you like?");
            string tempAmount = Console.ReadLine();
            int letterAmount = Convert.ToInt32(tempAmount);


            //Setup
            int score = 0;
            int counter = 0;

            string tempLetter = "";
            string outputString = "";
            string currentLetter = "";

            Random rnd = new Random();

            List<string> chosenLetters = new List<string>();
            string[] letters = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };


            //Get random letters from alphabet
            for (int i = 1; i <= letterAmount; i++)
            {
                tempLetter = letters[rnd.Next(0, letters.Length)];
                chosenLetters.Add(tempLetter);
                outputString += tempLetter;
                tempLetter = "";
            }


            //Output the chosen letters
            Console.WriteLine(outputString);

            while (score < letterAmount)
            {
                for (int i = 1; i <= letterAmount; i++)
                {
                    tempLetter = letters[rnd.Next(0, letters.Length)];
                    currentLetter = chosenLetters[counter];
                    Console.WriteLine(currentLetter + tempLetter);
                    counter++;
                    if (currentLetter == tempLetter)
                    {
                        score++;
                    }
                }

                Console.WriteLine(score);
            }
            
            else
            {
                Console.WriteLine("I got all of them correct");
            }
            

            Console.ReadKey();
        }
    }
}
&#13;
&#13;
&#13;

此外,我的代码可能非常草率和无效但我只是想让这个错误消息消失。

3 个答案:

答案 0 :(得分:0)

这是有问题的代码:

else
{
    Console.WriteLine("I got all of them correct");
}

如果没有前面的if语句,则不能有else子句:

if(someCondition)
{
}
else
{
}

希望这有帮助

答案 1 :(得分:0)

问题在于else循环后错误放置的while子句。在查看您的代码后,我相信您可能在此之前无意中删除了if,或者您可以将else更改为其他内容或将其全部删除。

// Could something be missing here...
if(score != letterAmount)
{
    // Do whatever here
}
else
{
    Console.WriteLine("I got all of them correct");
}

答案 2 :(得分:-1)

我听说过if...else表达,但从来没有while...else表达。

else必须与if声明匹配。