首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#中的多个错误难以解决

c#中的多个错误难以解决
EN

Stack Overflow用户
提问于 2020-08-13 05:30:04
回答 3查看 49关注 0票数 0
代码语言:javascript
复制
using System;

namespace VSCode
{
 class Program
 {
    static void Main()
    {
        //Terminal Settings
        Console.Title = "Dungeon Crawler";
        Console.WindowHeight = 40;
        Console.WindowWidth = 150;

        //vars
        int playerHP = 10;
        string armorType = "Leather Tunic";
        int armorValue = 1;
        string weaponType = "Wooden sword";
        int weaponDMG = 2;
        int gold = 100;
        string enemyType;
        int enemyHP;
        int enemyArmor;
        int enemyDMG;

        Console.WriteLine("Type 'start' to start the game");

        if(Console.ReadLine() == "start")
        {
            DungeonCrawlersIntro();
        }

        void DungeonCrawlersIntro()
        {
            //intro
            Console.WriteLine("---Welcome To Dungeon Crawler!");
            Console.WriteLine("---Press Any Key to get the next line---");
            Console.ReadKey(true);
            Console.WriteLine("---Dungeon Crawler is a game made by YaLocalJesterBoi AKA Addi-O---");
            Console.ReadKey(true);
            Console.WriteLine("---It is a simple pure text RPG that I am working on alone with minimal yt help and the like. its just me making this thing for fun---");
            Console.ReadKey(true);
            Console.WriteLine("---Anyways, enjoy!");
            Console.ReadKey(true);
            Console.WriteLine("---You are an adventurer who has come from far away in order defeat the dungeon of the gods, 'Malakeith'---");
            Console.ReadKey(true);
            Console.WriteLine("--Like most other adventurers, all you want is money and loot. To be the strongest of them all---");
            Console.ReadKey(true);
            Console.WriteLine("---Currently you have " + playerHP + " HP " + armorType + " armor type that gives " + armorValue + " armor and " + gold + " Gold along with a " + weaponType + " that deals " + weaponDMG + " Attack damage---");                
            Console.ReadKey(true);
            Console.WriteLine("---The dungeon Malakeith is quite famous and has a dfficulty all the from 'F' rank adventurers adventuring, to S class adventurers comeing to beat the dungeon entirely---");
            Console.ReadKey(true);
            Console.WriteLine("---You, just like many other ambitious adventurers just want some money and loot to live your life lavishly---");
            Console.ReadKey(true);
            Console.WriteLine("---The Dungeon itself is extremely popular, garnering people from all pver the world to explore it, but the selling point isn't just the dungeon itself, but the city that has been created around it. Malakeith city is well known as the best place to buy and sell anything obtained, or used in adventuring, all the way from a godly sword, to a simple health potion sold by every peddler---");
            Console.ReadKey(true);
            Console.WriteLine("---Type '/dungeon' to go to the dungeon---");
            Console.WriteLine("---If you dont comply the game will simply shut down---");

            if(Console.ReadLine() == "/dungeon")
            {
                Dungeon();
            }

            else
            {
                Console.WriteLine("---Since you were messing around, you got mugged and killed---");
                Console.ReadKey();
                playerHP = playerHP - 10;
            
                if(playerHP < 1)
                {
                    return;
                }
            }   
        }

        void Dungeon()
        {
            Console.WriteLine("---You have entered the very first floor of the Malakeith dungeon!---");
            Console.ReadKey(true);
            Console.WriteLine("As you enter, you get transported into an iteration of the dungeon, totally randomized each time for every adventurer or party---");
            Console.ReadKey(true);
            Console.WriteLine("---The inside seem to be meadows, stretching on beyond the horizon---");
            Console.ReadKey(true);
            Console.WriteLine("---The only residents of this area are slimes and some other petty creatures such as goblins and the occasional goblin leader---");
            Console.ReadKey(true);
            Console.WriteLine("---One such resident of this area has decided to have a shit at killing you---");
            Console.ReadKey(true);
            enemyRoll();
        }

        void enemyRoll()
        {
            Random enemyRollValue = new Random();

            int roll = 0;
            roll = enemyRollValue.Next(1,7);

            while(roll > 3)
            {
                goblinFight();
            }

            else
            {
                slimeFight();
            }
        }
        
        void goblinFight()
        {
            enemyType = "goblin";
            enemyHP = 5;
            enemyArmor = 0;
            enemyDMG = 4;

            Console.WriteLine("---This resident that has come to fight is a " + enemyType + "---");
            Console.ReadKey(true);
            Console.WriteLine("It has " + enemyHP + " HP");
            Console.ReadKey(true);
            Attack();
        }

        void slimeFight()
        {
            enemyType = "slime";
            enemyHP = 3;
            enemyArmor = 0;
            enemyDMG = 2;

            Console.WriteLine("---This resident that has come to fight is a " + enemyType + "---");
            Console.ReadKey(true);
            Console.WriteLine("It has " + enemyHP + " HP");
            Console.ReadKey(true);
        }

        void Attack()
        {
            enemyHP = (enemyHP + armorValue) - (weaponDMG + extraATK);
        }

        void AddAttack()
        {
            Random addAttack = new Random();

            int extraATKRoll = 0;
            extraATKRoll = addAttack.Next(1,10);

            while(extraATKRoll >= 5)
            {
                public static int extraATK = 1;
            }
        }
    }
}
}

这是100多行代码,我为我的宠物项目,地牢爬虫。这只是一个简单的纯文本VSCode游戏,我一直在做,以提高我的技能和打字速度,使我只是更舒适的VSCode。有几个错误我似乎找不到答案。我试过在网上找到的东西,但没有用。错误消息如下:

}预期VSCode }预期的VSCode、VSCode、VSCode类型或名称空间定义,或文件末尾预期的VSCode类型或名称空间定义,或文件末尾预期的VSCode类型或命名空间定义,或文件末尾预期的VSCode类型或名称空间定义,或文件末尾期望的VSCode变量“enemyArmor”,但它的值从未使用过VSCode变量“enemyDMG”,但它的值从未使用过VSCode,本地函数'AddAttack‘被声明,但从未使用VSCode。

与敌人的装甲奇怪的是,还有其他的ints和字符串是声明和使用在完全相同的地方,没有区别(我看到)在使用或写作。主要的问题是'}‘,因为以前唯一的问题是,由于我搞砸了,需要增加1个'}’,但在那之后,随着我编写了越来越多的代码,出现了更多的‘}’。我尽我所能找到,但没能走得更远。我已经复制了一些代码,这些代码是我在游戏的前一次迭代中编写的,我已经放弃了太多的sphaghetti。

一定要记住,我对编码很陌生,而且不太擅长编码,所以请把你的答案用一个新的编码器能理解的方式来回答。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-08-13 05:46:48

首先,您有以下内容;您不能else一个while语句

代码语言:javascript
复制
while(roll > 3)
{
    goblinFight();
}

//Comment out this mess, or do something else with it
//else
//{
//    slimeFight();
//}

其次是,您试图在while语句中声明静态成员。再说一次,这是毫无意义的:

代码语言:javascript
复制
while(extraATKRoll >= 5)
{
   // this doesn't make sense, comment it out
   //public static int extraATK = 1;
}

您需要将它直接放在类中(至少):

代码语言:javascript
复制
class Program
{
   public static int extraATK = 1;
票数 0
EN

Stack Overflow用户

发布于 2020-08-13 05:49:14

让我们一次看一遍:

代码语言:javascript
复制
void enemyRoll()
{
    Random enemyRollValue = new Random();

    int roll = 0;
    roll = enemyRollValue.Next(1,7);

    while(roll > 3)
    {
        goblinFight();
    }

    else
    {
        slimeFight();
    }
}

有一个不以else块作为前缀的if。将时间更改为if块以解决问题

代码语言:javascript
复制
void enemyRoll()
{
    Random enemyRollValue = new Random();

    int roll = 0;
    roll = enemyRollValue.Next(1,7);

    if(roll > 3)
    {
        goblinFight();
    }
    else
    {
        slimeFight();
    }
}

下一个错误:

代码语言:javascript
复制
void AddAttack()
{
    Random addAttack = new Random();

    int extraATKRoll = 0;
    extraATKRoll = addAttack.Next(1,10);

    while(extraATKRoll >= 5)
    {
        public static int extraATK = 1;
    }
}

不能在方法中创建全局变量(即extraATK)。像extraATK一样在int enemyDMG下面定义int extraATK = 0;

在将while转换为if之后,函数应该如下所示:

新功能应该如下所示:

代码语言:javascript
复制
void AddAttack()
{
    Random addAttack = new Random();

    int extraATKRoll = 0;
    extraATKRoll = addAttack.Next(1,10);

    if(extraATKRoll >= 5)
    {
        extraATK = 1;
    }
}

这应该能解决问题。

票数 1
EN

Stack Overflow用户

发布于 2020-08-13 05:46:58

enemyRoll方法中,事情就像梨形一样。您有一个while语句,后面跟着一个else语句。您似乎在某个地方缺少了一个if,但是不可能确切地知道您在那里的意图是什么。

while更改为if解决了这个问题,但您仍然有另一个问题。在AddAttack方法中,您有另一个while循环,并在其中尝试声明一个static字段,这一点都没有意义。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63388667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档