c# - 标识符应为 c# 未知原因

标签 c# compiler-errors

尝试制作一个简单的应用程序,它会问几个问题。
但由于某种原因,我的 AskQuestion 功能不起作用。
我计划稍后添加一个易于交换的数据库,这就是为什么我尝试采用更模块化的方法,因为我是初学者,我不确定我做错了什么。唯一的错误在 AskQuestion 类的第 21 行。
错误是:

CS1001 Identifier Expected

CS1514 { expected

CS1513 } expected

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.");
        }
    }
}

最佳答案

改变这个

class void AskQuestion(Question Q)


void AskQuestion(Question Q)

这应该是一种方法。关键字class告诉编译器你想在 out 类中创建一个内部类 Program

关于c# - 标识符应为 c# 未知原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54890547/

相关文章:

c# - LINQ 扩展如何链接其他扩展

c# - 无法导入.NET Core 3.0加载的DLL

c++ - 内联函数和静态内联函数之间的区别

java - Java : Cannot find symbol? [duplicate]

c# - 在C#中发布: Identifier expected

c# - 如何使用 Join 和 Where 返回 IList?

c# - CngKey.Create 不支持请求的操作

c - C中的字符问题

r - 在ubuntu上编译R包,ELF头无效

C# 使用位图 LockBits 处理 bmp 图像。尝试更改所有字节,但保存后其中一些字节的值仍然为 0。为什么?