【问题标题】:C# 'Unassigned local variable'?C#'未分配的局部变量'?
【发布时间】:2012-12-31 11:32:06
【问题描述】:

我尝试编写一段代码,该代码最终会掷骰子来决定哪个角色获胜,并且一直说有错误(使用未分配的局部变量'skillmodifier') &(使用未分配的局部变量“strengthmodifier”)。我真的很感激任何帮助。 P.S 我只在 Visual Studio 2010 上做了很短的一段时间的编程。请帮我找到解决这个问题的方法,出现问题是因为我使用 变量strengthmodifier”和“skillmodifier两次。谢谢你,你忠实的,维卡什。 我将粘贴下面的任务 breif 和之后的代码:

任务 3 确定遭遇的结果 当两个角色相遇时,结果由以下决定 进程:

• 计算两个角色的强度属性之间的差异

• 此差除以 5,然后向下舍入以创建“强度修正”

• 对技能属性重复该过程以创建“技能修饰符”

• 每个玩家投掷一个 6 面骰子。

• 如果两个骰子的分数相同,则不做任何更改

• 如果分数不相同,则得分最高的玩家将增加“力量” 修饰符”为力量值,“技能修饰符”为他们的技能值 人物

• 骰子得分较低的玩家从 他们角色的力量和技能值

• 如果技能值变为负数,则将其存储为零

• 如果力量值变为零或负数,则角色死亡。

程序应该:

*• 允许用户输入两个角色的力量和技能。

• 使用上述过程显示遭遇的结果。 设计一个算法来描述这个过程。编写、测试和评估代码。*

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

        namespace Task3
       {
         class Program
          {

            static void loopfsto()
             {

            Console.WriteLine("Please enter a value of strength, and then press enter");
            string csto = Console.ReadLine(); // Read string from console
            int csto1;
            if (int.TryParse(csto, out csto1)) // Try to parse the string as an integer
            {
                Console.WriteLine("Your chosen number is " + csto1 + ".");
                Console.ReadKey();
            }
            else
            {
                Console.Clear();
                Console.WriteLine("Not an integer!");
                Console.ReadKey();
                Console.Clear();
                loopfsto();
            }
        }

        static void loopfsko()
        {
            Console.WriteLine("Please enter a value of skill, and then press enter");
            string csko = Console.ReadLine(); // Read string from console
            int csko1;
            if (int.TryParse(csko, out csko1)) // Try to parse the string as an integer
            {
                Console.WriteLine("Your chosen number is " + csko1 + ".");
                Console.ReadKey();
            }
            else
            {
                Console.Clear();
                Console.WriteLine("Not an integer!");
                Console.ReadKey();
                Console.Clear();
                loopfsko();
            }
            Console.Clear();
        }

        static void loopfstt()
        {
            Console.WriteLine("Please enter a value of strength for, and then press enter");
            string cstt = Console.ReadLine(); // Read string from console
            int cstt1;
            if (int.TryParse(cstt, out cstt1)) // Try to parse the string as an integer
            {
                Console.WriteLine("Your chosen number is " + cstt1 + ".");
                Console.ReadKey();
            }
            else
            {
                Console.Clear();
                Console.WriteLine("Not an integer!");
                Console.ReadKey();
                Console.Clear();
                loopfstt();
            }
            Console.Clear();
        }

        static void loopfskt()
        {
            Console.WriteLine("Please enter a value of skill for, and then press enter");
            string cskt = Console.ReadLine(); // Read string from console
            int cskt1;
            if (int.TryParse(cskt, out cskt1)) // Try to parse the string as an integer
            {
                Console.WriteLine("Your chosen number is " + cskt1 + ".");
                Console.ReadKey();
            }
            else
            {
                Console.Clear();
                Console.WriteLine("Not an integer!");
                Console.ReadKey();
                Console.Clear();
                loopfskt();
            }

        }

        static void Main(string[] args)
        {

            string Character1;
            string Character2;
            int strengthmodifiertoround;
            int skillmodifiertoround;
            int strengthmodifier;
            int skillmodifier;
            Console.Title = "Strength and Skill";

            Console.WriteLine("Welcome to Strength and Skill, please press enter to continue.");
            Console.ReadKey();
            Console.Clear();

            Console.WriteLine("Please enter a name for character 1, then press enter.");
            Character1 = Console.ReadLine();
            Console.Clear();

            Console.WriteLine("Please enter a name for character 2, then press enter.");
            Character2 = Console.ReadLine();
            Console.Clear();

            Console.WriteLine("Please enter a value of strength for " + Character1 + ", and then press enter");
            string csto = Console.ReadLine(); // Read string from console
            int csto1;
            if (int.TryParse(csto, out csto1)) // Try to parse the string as an integer
            {
                Console.WriteLine("Your chosen number is " + csto1 + ".");
                Console.ReadKey();
            }
            else
            {
                Console.Clear();
                Console.WriteLine("Not an integer!");
                Console.ReadKey();
                Console.Clear();
                loopfsto();
            }


            Console.Clear();


            Console.WriteLine("Please enter a value of skill for " + Character1 + ", and then press enter");
            string csko = Console.ReadLine(); // Read string from console
            int csko1;
            if (int.TryParse(csko, out csko1)) // Try to parse the string as an integer
            {
                Console.WriteLine("Your chosen number is " + csko1 + ".");
                Console.ReadKey();
            }
            else
            {
                Console.Clear();
                Console.WriteLine("Not an integer!");
                Console.ReadKey();
                Console.Clear();
                loopfsko();
            }
            Console.Clear();

            Console.WriteLine(Character1 + " has a strength of " + csto1 + " and a skill of " + csko1 + ".");
            Console.ReadKey();
            Console.Clear();

            Console.WriteLine("Please enter a value of strength for " + Character2 + ", and then press enter");
            string cstt = Console.ReadLine(); // Read string from console
            int cstt1;
            if (int.TryParse(cstt, out cstt1)) // Try to parse the string as an integer
            {
                Console.WriteLine("Your chosen number is " + cstt1 + ".");
                Console.ReadKey();
            }
            else
            {
                Console.Clear();
                Console.WriteLine("Not an integer!");
                Console.ReadKey();
                Console.Clear();
                loopfstt();
            }
            Console.Clear();



            Console.WriteLine("Please enter a value of skill for " + Character2 + ", and then press enter");
            string cskt = Console.ReadLine(); // Read string from console
            int cskt1;
            if (int.TryParse(cskt, out cskt1)) // Try to parse the string as an integer
            {
                Console.WriteLine("Your chosen number is " + cskt1 + ".");
                Console.ReadKey();
            }
            else
            {
                Console.Clear();
                Console.WriteLine("Not an integer!");
                Console.ReadKey();
                Console.Clear();
                loopfskt();
            }
            Console.Clear();

            Console.WriteLine(Character2 + " has a strength of " + cstt1 + " and a skill of " + cskt1 + ".");
            Console.ReadKey();
            Console.Clear();
//--- Finds out if strength for character 1 is higher than 2 or vice versa. Then finds difference between two and makes a variable called strengthmodifier ---//

            {
                if (csto1 < cstt1)
                {
                    strengthmodifiertoround = cstt1 - csto1;
                    strengthmodifier = strengthmodifiertoround / 5;   
                }
                if (cstt1 < csto1)
                {
                    strengthmodifiertoround = csto1 - cstt1;
                    strengthmodifier = strengthmodifiertoround / 5;
                }
            }
//--- Finds out if skill for character 1 is higher than 2 or vice versa. Then finds difference between two and makes a variable called skillmodifier ---//

            {
                if (csko1 < cskt1)
                {
                    skillmodifiertoround = cskt1 - csko1;
                    skillmodifier = skillmodifiertoround / 5; 
                }
                if (cskt1 < csko1)
                {
                    skillmodifiertoround = csko1 - cskt1;
                    skillmodifier = skillmodifiertoround / 5;
                }
            }
//--- Tells user to put input and roll a virtual dice (which is actually creating a number between 1 and 6) ---//
            Console.WriteLine(Character1 + ", please press enter to roll dice");
            Console.ReadKey();
            Random rand = new Random();
            int character1RandomNumber = rand.Next(1, 6);

            Console.WriteLine(Character2 + ", please press enter to roll dice");
            Console.ReadKey();
            Random rand1 = new Random();
            int character2RandomNumber = rand1.Next(1, 6);
            Console.WriteLine(Character1 + " rolled a " + character1RandomNumber + " and " + Character2 + " rolled a " + character2RandomNumber + ".");
            Console.ReadKey();
            if (character1RandomNumber < character2RandomNumber)
            {
                int char2st = cstt1 + strengthmodifier;
                int char2sk = cskt1 + skillmodifier;
                int char1st = csto1 - strengthmodifier;
                int char1sk = csko1 - skillmodifier;
            }
            if (character2RandomNumber < character1RandomNumber)
            {

            }

                int ch2st = cstt1 - strengthmodifier;
                int ch2sk = cskt1 - skillmodifier;
                int ch1st = csto1 + strengthmodifier;
                int ch1sk = csko1 + skillmodifier;



        }
    }
}

【问题讨论】:

  • 看看使用 ReSharper (jetbrains.com/resharper),该工具将帮助您发现此类问题并为您的代码提供提示或建议。它只是突出显示代码行以指示您的代码存在问题的位置。
  • 任务 3 确定遭遇的结果 当两个角色之间发生遭遇时,结果由以下过程确定: • 计算两个角色的力量属性之间的差异
  • • 此差值除以 5,然后向下取整以创建“强度修正” • 对技能属性重复该过程以创建“技能修正” • 每个玩家投掷一个 6 面骰子. • 如果两个骰子的分数相同,则不做任何更改 • 如果分数不同,则得分最高的玩家将“力量修正”添加到力量值,将“技能修正”添加到技能值为他们的角色 • 骰子得分较低的玩家从他们角色的力量和技能值中减去这些修饰符
  • • 如果技能值变为负值,则将其存储为零 • 如果力量值变为零或负值,则角色死亡。该程序应该: • 允许用户输入两个字符的强度和技能。 • 使用上述过程显示遭遇的结果。设计一个算法来描述这个过程。编写、测试和评估代码。
  • 抱歉没有早点介绍。我是新手,希望你能理解。非常感谢您的帮助。

标签: c# visual-studio variables console


【解决方案1】:

实际上,我认为您的问题是因为技能修饰符和强度修饰符没有在所有代码路径上获得分配值。 IE。它们仅从 if 子句中分配值,并且 Visual Studio 无法确定它们是否分配给所有可能的结果。此警告不应停止您的代码编译,但如果您希望它消失,您可以执行类似的操作

int skillmodifiertoround = 0;

而不仅仅是

int skillmodifiertoround;

现在 Skillmodifiertoround 在声明时被赋予了一个值。

编辑:“此警告不应停止您的代码编译” - 显然它会阻止程序在 c# 中正确编译,但 vb 中的相同错误仅给出警告但仍可编译。

【讨论】:

  • 感谢您的帮助 5uperdan。它帮助我改变了另一个问题。
  • @vik1924 而不是if (csko1 &lt; cskt1) { ... } if (cskt1 &lt; csko1) { ... } 使用if (csko1 &lt; cskt1) { ... } else { ... }。这样编译器就会知道控制已经通过了两个块中的任何一个,然后它可以推断出在读取之前已经分配了局部变量。
【解决方案2】:

其实很简单

int skillmodifier;

您需要先分配它 - 即给它一个值,甚至是一个默认值 - 然后才能使用它。否则程序不知道它有什么价值。

类似

int skillmodifier = -1;

会帮你解决的。

【讨论】:

    【解决方案3】:

    当您声明 int skillmodifier 时,将其声明为默认值,例如int skillmodifier = 0;

    strengthmodifier 做同样的事情,你应该很高兴!

    问题是编译器可以检测到在不设置这些参数的情况下执行代码的方式。

    【讨论】:

    • 非常感谢 Ross Dargan,非常感谢您的帮助。
    【解决方案4】:

    C# 中的局部变量必须在使用前进行初始化。

    来自MSDN

    另见相关问题Why aren't unassigned local variables automatically initialized?

    对于类型未知的泛型,使用default关键字

    【讨论】:

    • 感谢 Tilak 的这一有用声明。我会努力克服错误。
    【解决方案5】:

    我想其他人已经给了你答案,但我发现了别的东西。

    而不是 //方法 如果(尝试解析) { //完毕 } 别的 //循环

    您应该将方法转换为返回整数的方法,然后...

    int csko = loopCsko()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-17
      • 2013-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 2014-05-13
      • 2015-03-18
      相关资源
      最近更新 更多