I am getting the following error. Use of unassigned local variable markduplicate. I don't understand why? This program finds a duplicate in an array. I been trying to figure it out and I feel like im so close. Thanks for the help.
我得到了下面的错误。使用未分配的局部变量markduplicate。我不明白为什么?这个程序在数组中找到一个副本。我一直在努力想弄清楚,我感觉自己离得太近了。谢谢你的帮助。
using System;
class duplicate
{
static void Main()
{
const int Array_Size = 5;
int [] number = new int [Array_Size];
int i;
for ( i = 0; i < Array_Size; i++)
{
Console.Write("Element " + i + ": ");
number[i] = Int32.Parse(Console.ReadLine());
if (number[i] < 9 || number[i] > 101)
{
Console.WriteLine("Enter Number between 10 - 100");
number[i] = Int32.Parse(Console.ReadLine());
}
}
bool markduplicate;
for (i = 0; i < Array_Size; i++)
{
for (int j = 0; j < Array_Size; j++)
{
if (i != j)
{
if (number[j] == number[i])
{
markduplicate = true;
}
}
if (markduplicate != true)
{
Console.WriteLine("Element " + i + " " + number[i]);
}
}
}
}
}
}
5 个解决方案
#1
4
This is because the C#'s code analyzer detected that there are paths through you program when the markduplicate
's value would be referenced before any assignments to it are made. Specifically, this is going to happen during the very first iteration of the nested loop, when both i
and j
are zero: the if (i != j)
block containing the assignment is not going to execute, so the value of the markduplicate
is going to get retrieved in the very next if
statement.
这是因为c#的代码分析器检测到,当markduplicate的值在赋值之前被引用时,存在通过您的程序的路径。具体地说,这是发生在第一个迭代中嵌套循环,当我和j都是零:如果(我! = j)块包含的任务是不会执行,所以markduplicate的价值将得到下一个if语句检索。
To fix this problem, change
要解决这个问题,改变
bool markduplicate;
to
来
bool markduplicate = false;
in the declaration.
的声明。
#2
1
The compiler thinks it is possible for you to hit the if (markduplicate != true)
line before markduplicate
has been set.
编译器认为,在设置markduplicate之前,您可以点击if (markduplicate != true)行。
If you think the compiler is wrong, give it a value when declaring e.g. bool markduplicate = true;
. If you analyze and think the compiler is correct, adjust you code accordingly.
如果您认为编译器是错误的,请在声明时给它一个值,例如bool markduplicate = true;如果您分析并认为编译器是正确的,请相应地调整您的代码。
Also: if (markduplicate != true)
is considered poor style. Use if(!markduplicate)
instead.
同样:if (markduplicate != true)被认为是糟糕的样式。如果(! markduplicate)而不是使用。
#3
0
You've declared the boolean variable markduplicate
but you haven't given it an initial value - the compiler doesn't know if it should be initially true or false.
您已经声明了布尔变量markduplicate,但是没有给它一个初始值——编译器不知道它最初是真还是假。
In this case it's clear you want it to be initially false, so put this:
在这种情况下,很明显你希望它一开始是假的,所以写下来:
bool markduplicate = false;
it will now compiled.
现在编译。
#4
0
In your code, (i != j)
is false before it is true and therefore you are checking the value of your variable which has had nothing assigned to it.
在您的代码中,(i != j)是假的,因此您正在检查您的变量的值,它没有分配给它。
You need to either assign a value to markduplicate at its declaration or you need to make sure that any path that leads to a conditional check on its value has a value assigned to it first.
您需要在markduplicate的声明中为其赋值,或者需要确保导致对其值进行条件检查的任何路径都首先赋值。
#5
0
You have to assign markdefault
to either true
or false
in your declaration. Otherwise, if number [j] != number [i]
, then markdefault
will not be assigned and if markduplicate != true
cannot be evaluated.
您必须在声明中为true或false分配markdefault。否则,如果number [j] != number [i],则不会分配markdefault;如果无法计算markduplicate != true。
#1
4
This is because the C#'s code analyzer detected that there are paths through you program when the markduplicate
's value would be referenced before any assignments to it are made. Specifically, this is going to happen during the very first iteration of the nested loop, when both i
and j
are zero: the if (i != j)
block containing the assignment is not going to execute, so the value of the markduplicate
is going to get retrieved in the very next if
statement.
这是因为c#的代码分析器检测到,当markduplicate的值在赋值之前被引用时,存在通过您的程序的路径。具体地说,这是发生在第一个迭代中嵌套循环,当我和j都是零:如果(我! = j)块包含的任务是不会执行,所以markduplicate的价值将得到下一个if语句检索。
To fix this problem, change
要解决这个问题,改变
bool markduplicate;
to
来
bool markduplicate = false;
in the declaration.
的声明。
#2
1
The compiler thinks it is possible for you to hit the if (markduplicate != true)
line before markduplicate
has been set.
编译器认为,在设置markduplicate之前,您可以点击if (markduplicate != true)行。
If you think the compiler is wrong, give it a value when declaring e.g. bool markduplicate = true;
. If you analyze and think the compiler is correct, adjust you code accordingly.
如果您认为编译器是错误的,请在声明时给它一个值,例如bool markduplicate = true;如果您分析并认为编译器是正确的,请相应地调整您的代码。
Also: if (markduplicate != true)
is considered poor style. Use if(!markduplicate)
instead.
同样:if (markduplicate != true)被认为是糟糕的样式。如果(! markduplicate)而不是使用。
#3
0
You've declared the boolean variable markduplicate
but you haven't given it an initial value - the compiler doesn't know if it should be initially true or false.
您已经声明了布尔变量markduplicate,但是没有给它一个初始值——编译器不知道它最初是真还是假。
In this case it's clear you want it to be initially false, so put this:
在这种情况下,很明显你希望它一开始是假的,所以写下来:
bool markduplicate = false;
it will now compiled.
现在编译。
#4
0
In your code, (i != j)
is false before it is true and therefore you are checking the value of your variable which has had nothing assigned to it.
在您的代码中,(i != j)是假的,因此您正在检查您的变量的值,它没有分配给它。
You need to either assign a value to markduplicate at its declaration or you need to make sure that any path that leads to a conditional check on its value has a value assigned to it first.
您需要在markduplicate的声明中为其赋值,或者需要确保导致对其值进行条件检查的任何路径都首先赋值。
#5
0
You have to assign markdefault
to either true
or false
in your declaration. Otherwise, if number [j] != number [i]
, then markdefault
will not be assigned and if markduplicate != true
cannot be evaluated.
您必须在声明中为true或false分配markdefault。否则,如果number [j] != number [i],则不会分配markdefault;如果无法计算markduplicate != true。