visual studio - c# '未分配局部变量'
本文关键字:分配 局部变量 studio visual | 更新日期: 2023-09-27 17:50:00
我试图制作一段代码,该代码最终会掷骰子来决定哪个角色获胜,并且它一直说有错误(使用未分配的局部变量'skillmodifier') &(使用未分配的局部变量'strengthmodifier')。我将非常感谢任何帮助。附注:我只在Visual Studio 2010上做了很短的一段时间的编程。请帮我找到解决这个问题的方法,问题的发生是因为我使用变量' strengthmodifier'和'skillmodifier' 两次。谢谢你,你忠实的,Vikash。我将粘贴下面的任务概要和后面的代码:
任务3确定相遇的结果当两个角色相遇时,结果是由以下因素决定的过程:
•计算两个字符的强度属性之间的差异
•该差异除以5,然后四舍五入创建一个"强度修饰符"
•对于技能属性重复此过程以创建"技能修改符"
•每个玩家掷一个六面骰子。
•如果两个骰子的分数相同,则不做任何更改
•如果得分不相同,得分最高的玩家增加"力量"Modifier"表示力量值,"skill Modifier"表示技能值字符
•骰子得分较低的玩家从角色的力量和技能值
•如果技能值变为负值,那么它将被存储为零•如果强度值变为零或负值,则该角色死亡。
*•允许用户输入两个角色的力量和技能。
•使用上述过程显示遇到的结果。设计一个算法来描述这个过程。编写、测试和评估代码*
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;
}
}
}
实际上,我认为你的问题是因为技能modifier和强度modifier没有在所有代码路径上获得分配值。ie。它们只在if子句中赋值,visualstudio无法确定是否为所有可能的结果赋值。这个警告不应该阻止你的代码编译,但如果你想让它消失,你可以这样做
int skillmodifiertoround = 0;
而不是
int skillmodifiertoround;
现在skillmodifierround在声明时被赋值。
编辑:"这个警告不应该阻止你的代码编译" -显然它会阻止程序在c#中正确编译,但在vb中同样的错误只给出一个警告,但仍然编译。
这真的很简单
int skillmodifier;
在使用它之前,你需要给它赋值——也就是给它一个值,即使是默认值。否则程序不知道它的值是什么。
比如
int skillmodifier = -1;
当你声明int skillmodifier
时,声明它为默认值,例如:int skillmodifier = 0;
对strengthmodifier
做同样的事情,你应该很好去!
问题是编译器可以在没有设置这些参数的情况下检测到执行代码的方法。
c#中的从MSDN局部变量在使用之前必须初始化。
请参见相关问题为什么未赋值的局部变量不自动初始化?
对于类型未知的泛型,使用默认关键字
我想其他人已经给你答案了,但是我发现了别的东西。
代替//方法如果(tryparse){//完成}其他的//循环
你应该把这些方法变成返回int的方法,然后…
int csko = loopCsko()