未知原因的预期C#标识符

时间:2019-02-26 16:57:37

标签: c# compiler-errors

尝试制作一个简单的应用程序,它将询问一些问题。 但是由于某种原因,我的AskQuestion函数不起作用。 我计划以后再添加一个易于交换的数据库,这就是为什么我尝试采用稍微模块化的方法的原因,并且由于我是初学者,所以我不确定自己做错了什么。唯一的错误是AskQuestion类的第21行。

错误是:

  

期望使用CS1001标识符

     

CS1514 {预期

     

CS1513}预期

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

namespace Quiz
{
    class Program
    {
        // Question Base
        class Question
        {
            public String question = "Empty Question";
            public String correctanswer = "Empty Answer";
            public String givenanswer = "Empty Answer";
            public String response = "Empty Response.";
            public bool cleared = false;
        }
        // Ask Base
        class void AskQuestion(Question Q)
        {
            while (Q.cleared == false)
            {
                Console.WriteLine(Q.question);
                Q.givenanswer = Console.ReadLine();
                Q.givenanswer.ToLower();
                if (Q.givenanswer == Q.correctanswer)
                {
                    Console.WriteLine(Q.response);
                    Q.cleared = true;
                }
                else
                {
                    Console.WriteLine("Wrong. Try again.");
                }
            }

        }

        // Main Function
        void Main(string[] args)
        {
            string Name;
            Console.WriteLine("Welcome challenger! You're going to have a good time.");
            Console.WriteLine("Make sure you use proper grammar. Or you may be stuck for no reason.");
            Console.WriteLine("What is your name challenger?");
            Name = Console.ReadLine();
            Console.WriteLine("Welcome {0} to the challenge. I wish you best of luck. You will need it.",Name);
            Question Q1 = new Question();
            Q1.question = "What is the color of the sun?";
            Q1.correctanswer = "White";
            Q1.response = "Correct. Despite the fact it appears Yellow on earth, if you observe the sun from space, you would see it's true color. White.";
            AskQuestion(Q1);
            Q1.cleared = true;
            Console.WriteLine("Nice little warmup. But, lets get a bit serious.");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

更改此

class void AskQuestion(Question Q)

void AskQuestion(Question Q)

这应该是一种方法。关键字class告诉编译器您要在out类Program

中创建一个内部类

答案 1 :(得分:0)

Q.givenanswer.ToLower();不会使Q.givenanswer小写-它返回一个新的小写字符串,您需要将其分配给变量,或者只是`Q.givenanswer = Q.givenanswer.ToLower(); < / p>