Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类型或命名空间定义,或应为文件结尾。_C# - Fatal编程技术网

C# 类型或命名空间定义,或应为文件结尾。

C# 类型或命名空间定义,或应为文件结尾。,c#,C#,我是C#的新手,我正在学习如何使用一本书。但是在创建这个之后(粘贴在另一个解决方案的代码中)。数学题是从另一个解决方案粘贴的,是的,我知道我不应该使用goto,但我稍后会停止。我想我有太多的“{}”,但我似乎找不到正确的区域。如果我的错误很愚蠢,请原谅 using System; namespace LearningCSharp { class MainClass { public static void Main(string[] args) {

我是C#的新手,我正在学习如何使用一本书。但是在创建这个之后(粘贴在另一个解决方案的代码中)。数学题是从另一个解决方案粘贴的,是的,我知道我不应该使用goto,但我稍后会停止。我想我有太多的“{}”,但我似乎找不到正确的区域。如果我的错误很愚蠢,请原谅

using System;

namespace LearningCSharp
{
    class MainClass
    {
    public static void Main(string[] args)
    {
        //math
        bool correct = true;
        bool incorrect = false;
        int q1;
        int q2;
        int q3;
        double q4;
        int q5;
        int q6;
        int q7;
        int q8;
        int q9;
        int q10;
        int q11;
        int q12;
        int q13;
        int q14;
        int q15;
        int restart;
        int mainMenuDivisionTest;

        //random
        Random rand = new Random(); //spawning instance of a class
        int randomOne = rand.Next(1,11); //max value is 10 non inclusive
        int randomeTwo = rand.Next(1,11);

        //main menu
        int chooseArea;
        int divisionTest;
        int divisionCalculator;
        int additionCalculator;
        int subtractionCalculator;
        int multiplicationCalculator;

        //Calculators
        int divisionCalculatorA1;
        int divisionCalculatorA2;
        int additionCalculatorA1;
        int additionCalculatorA2;
        int subtractionCalculatorA1;
        int subtractionCalculatorA2;

        //Checker
        int checker;

        MainMenu:
        Console.WriteLine("Press 0 then type enter for a division test.\nPress 1 then type enter for a division calculator.\nPress 2 then type enter for a addition calculator.\nPress 3 then type enter for a subtraction calculator\nPress 4 then type enter for a multiplication calculator");
        chooseArea = Convert.ToInt32(Console.ReadLine());

        if (chooseArea == 0) {
            goto Start;
            BadAnswer:
            Console.WriteLine("You're answer is " + incorrect + ", better luck next time!");
            Console.Write("Press 1 then enter to restart.\nPress 0 then enter to shutdown.\nPress 2 then enter to enter main menu.\nNumber: ");
            restart = Convert.ToInt32(Console.ReadLine());
            if (restart == 1)
            {
                goto Start;
            }
            else if (restart == 0)
            {
                System.Environment.Exit(1);
            }
            else if (restart == 2)
            {
                goto MainMenu;
            }
            else
            {
                System.Environment.Exit(1);
            }

            Start:
            Console.WriteLine("Please do not enter blank spaces, enter letters, and answers with spaces. \nOtherwise the program will crash.");
            Console.Write("What is 10 divided by 2?  ");
            q1 = Convert.ToInt32(Console.ReadLine());
            if (q1 == 5)
            {
                Console.WriteLine(correct);
                Console.WriteLine("Q1 Complete: Good Job, lets move to the next question.");
                Console.WriteLine("Press any key to continue to the next question.\n");
                Console.ReadLine();

                Console.Write("What is 50 divided by 25?  ");
                q2 = Convert.ToInt32(Console.ReadLine());
                if (q2 == 2)
                {
                    Console.WriteLine(correct);
                    Console.WriteLine("Q2 Complete: Good Job, lets move to the next question.");
                    Console.WriteLine("Press any key to continue to the next question.\n");
                    Console.ReadLine();

                    Console.Write("What is 100 divided by 10?  ");
                    q3 = Convert.ToInt32(Console.ReadLine());
                    if (q3 == 10)
                    {
                        Console.WriteLine(correct);
                        Console.WriteLine("Q3 Complete: Good Job, lets move to the next question.");
                        Console.WriteLine("Press any key to continue to the next question.\n");
                        Console.ReadLine();

                        Console.Write("What is 5 divided by 2?  ");
                        q4 = Convert.ToDouble(Console.ReadLine());
                        if (q4 == 2.5)
                        {
                            Console.WriteLine(correct);
                            Console.WriteLine("Q4 Complete: Good Job, lets move to the next question.");
                            Console.WriteLine("Press any key to continue to the next question.\n");
                            Console.ReadLine();

                            Console.Write("What is 600 divided by 2?  ");
                            q5 = Convert.ToInt32(Console.ReadLine());
                            if (q5 == 300)
                            {
                                Console.WriteLine(correct);
                                Console.WriteLine("Q5 Complete: Good Job, lets move to the next question.");
                                Console.WriteLine("Press any key to continue to the next question.\n");
                                Console.ReadLine();

                                Console.Write("What is 5,000 divided by 5?  ");
                                q6 = Convert.ToInt32(Console.ReadLine());
                                if (q6 == 1000)
                                {
                                    Console.WriteLine(correct);
                                    Console.WriteLine("Q6 Complete: Good Job, lets move to the next question.");
                                    Console.WriteLine("Press any key to continue to the next question.\n");
                                    Console.ReadLine();

                                    Console.Write("What is 4,000 divided by 8?  ");
                                    q7 = Convert.ToInt32(Console.ReadLine());
                                    if (q7 == 500)
                                    {
                                        Console.WriteLine(correct);
                                        Console.WriteLine("Q7 Complete: Good Job, lets move to the next question.");
                                        Console.WriteLine("Press any key to continue to the next question.\n");
                                        Console.ReadLine();

                                        Console.Write("What is 1,600 divided by 4?  ");
                                        q8 = Convert.ToInt32(Console.ReadLine());
                                        if (q8 == 400)
                                        {
                                            Console.WriteLine(correct);
                                            Console.WriteLine("Q8 Complete: Good Job, lets move to the next question.");
                                            Console.WriteLine("Press any key to continue to the next question.\n");
                                            Console.ReadLine();

                                            Console.Write("What is 200 divided by 4?  ");
                                            q9 = Convert.ToInt32(Console.ReadLine());
                                            if (q9 == 50)
                                            {
                                                Console.WriteLine(correct);
                                                Console.WriteLine("Q9 Complete: Good Job, lets move to the next question.");
                                                Console.WriteLine("Press any key to continue to the next question.\n");
                                                Console.ReadLine();

                                                Console.Write("What is 6,000 divided by 20?  ");
                                                q10 = Convert.ToInt32(Console.ReadLine());
                                                if (q10 == 300)
                                                {
                                                    Console.WriteLine(correct);
                                                    Console.WriteLine("Q10 Complete: Good Job, lets move to the next question.");
                                                    Console.WriteLine("Press any key to continue to the next question.\n");
                                                    Console.ReadLine();

                                                    Console.Write("What is 60 divided by 300?  ");
                                                    q11 = Convert.ToInt32(Console.ReadLine());
                                                    if (q11 == 0.2)
                                                    {
                                                        Console.WriteLine(correct);
                                                        Console.WriteLine("Q11 Complete: Good Job, lets move to the next question.");
                                                        Console.WriteLine("Press any key to continue to the next question.\n");
                                                        Console.ReadLine();

                                                        Console.Write("What is 1,000 divided by 100?  ");
                                                        q12 = Convert.ToInt32(Console.ReadLine());
                                                        if (q12 == 10)
                                                        {
                                                            Console.WriteLine(correct);
                                                            Console.WriteLine("Q12 Complete: Good Job, lets move to the next question.");
                                                            Console.WriteLine("Press any key to continue to the next question.\n");
                                                            Console.ReadLine();

                                                            Console.Write("What is 100,000 divided by 50,000?  ");
                                                            q13 = Convert.ToInt32(Console.ReadLine());
                                                            if (q13 == 2)
                                                            {
                                                                Console.WriteLine(correct);
                                                                Console.WriteLine("Q13 Complete: Good Job, lets move to the next question.");
                                                                Console.WriteLine("Press any key to continue to the next question.\n");
                                                                Console.ReadLine();

                                                                Console.Write("What is 1,000,000 divided by 50,000?  ");
                                                                q14 = Convert.ToInt32(Console.ReadLine());
                                                                if (q14 == 20)
                                                                {
                                                                    Console.WriteLine(correct);
                                                                    Console.WriteLine("Q14 Complete: Good Job, lets move to the next question.");
                                                                    Console.WriteLine("Press any key to continue to the next question.\n");
                                                                    Console.ReadLine();

                                                                    Console.Write("What is 20,000 divided by 320?  ");
                                                                    q15 = Convert.ToInt32(Console.ReadLine());
                                                                    if (q15 == 62)
                                                                    {
                                                                        Console.WriteLine(correct);
                                                                        Console.WriteLine("Q15 Complete: Good Job, you have finished everything! Score 15/15");
                                                                        Console.Write("Press 1 then enter to restart.\nPress 0 then enter to shutdown.\nPress 2 then enter to enter main menu.\nNumber: ");
                                                                        restart = Convert.ToInt32(Console.ReadLine());
                                                                        if (restart == 1)
                                                                        {
                                                                            goto Start;
                                                                        }
                                                                        else if (restart == 0)
                                                                        {
                                                                            System.Environment.Exit(1);
                                                                        }
                                                                        else if (restart == 2)
                                                                        {
                                                                            goto MainMenu;
                                                                        }
                                                                        else
                                                                        {
                                                                            System.Environment.Exit(1);
                                                                        }
                                                                    }

                                                                    else
                                                                    {
                                                                        Console.WriteLine("Score: 14/15");
                                                                        goto BadAnswer;
                                                                    }
                                                                }

                                                                else
                                                                {
                                                                    Console.WriteLine("Score: 13/15");
                                                                    goto BadAnswer;
                                                                }
                                                            }

                                                            else
                                                            {
                                                                Console.WriteLine("Score: 12/15");
                                                                goto BadAnswer;
                                                            }
                                                        }

                                                        else
                                                        {
                                                            Console.WriteLine("Score: 11/15");
                                                            goto BadAnswer;
                                                        }
                                                    }

                                                    else
                                                    {
                                                        Console.WriteLine("Score: 10/15");
                                                        goto BadAnswer;
                                                    }
                                                }

                                                else
                                                {
                                                    Console.WriteLine("Score: 9/15");
                                                    goto BadAnswer;
                                                }
                                            }

                                            else
                                            {
                                                Console.WriteLine("Score: 8/15");
                                                goto BadAnswer;
                                            }
                                        }

                                        else
                                        {
                                            Console.WriteLine("Score: 7/15");
                                            goto BadAnswer;
                                        }
                                    }

                                    else
                                    {
                                        Console.WriteLine("Score: 6/15");
                                        goto BadAnswer;
                                    }
                                }

                                else
                                {
                                    Console.WriteLine("Score: 5/15");
                                    goto BadAnswer;
                                }
                            }

                            else
                            {
                                Console.WriteLine("Score: 4/15");
                                goto BadAnswer;
                            }
                        }

                        else
                        {
                            Console.WriteLine("Score: 3/15");
                            goto BadAnswer;
                        }
                    }

                    else
                    {
                        Console.WriteLine("Score: 2/15");
                        goto BadAnswer;
                    }
                }

                else
                {
                    Console.WriteLine("Score: 1/15");
                    goto BadAnswer;
                }
            }

            else
            {
                Console.WriteLine("Score: 0/15");
                goto BadAnswer;

            }

                if (chooseArea == 1) {
                Console.WriteLine("Press Enter to Continue to Division Calculator");
                Console.ReadKey();
                DivisionStart:
                Console.WriteLine("Write Number (1): ");
                divisionCalculatorA1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Write Number (2): ");
                divisionCalculatorA2 = Convert.ToInt32(Console.ReadLine());

                ChooseNumberD:
                Console.WriteLine("Is you're question " + divisionCalculatorA1 + "divided by " + divisionCalculatorA2 + "?\n0 for No\n 1 for Yes");
                checker = Convert.ToInt32(Console.ReadLine());
                if (checker == 0)
                {
                    goto DivisionStart;
                }
                else if (checker == 1)
                {
                    Console.WriteLine("The answer is: " + divisionCalculatorA1 / divisionCalculatorA2);
                    Console.WriteLine("Would you like to do another division question?\n0 for main menu\n 1 for Yes\n 2 for Close Application");
                    checker = Convert.ToInt32(Console.ReadLine());
                    if (checker == 0)
                    {
                        goto MainMenu;
                    }
                    else if (checker == 1)
                    {
                        goto DivisionStart;
                    }
                    else if (checker == 2)
                    {
                        System.Environment.Exit(1);
                    }
                }
                else
                {
                    Console.WriteLine("Please choose a number.");
                    goto ChooseNumberD;
                }
            }
        }
    }
  }
}

正如您自己看到的,非常不可读的代码。。。考虑将其重新格式化为不同的函数和类……我复制了
Main
method的主体&它工作得很好。。。那么,这仅仅是因为缺少或增加了右大括号
{
}
而造成的打字错误吗?像这样的代码格式很差,很难找到不匹配的偏执。在VisualStudio中-如果将光标放在开始{上,则匹配的结束}将高亮显示。有点乏味和费时-但这里的大多数人不太可能为您这样做。正如您所看到的,非常不可读的代码。。。考虑将其重新格式化为不同的函数和类……我复制了
Main
method的主体&它工作得很好。。。那么,这仅仅是因为缺少或增加了右大括号
{
}
而造成的打字错误吗?像这样的代码格式很差,很难找到不匹配的偏执。在VisualStudio中-如果将光标放在开始{上,则匹配的结束}将高亮显示。有点乏味和费时,但这里的大多数人不太可能为你这样做。