[英]Array printing wrong results C# Console
My code suppos to print an array of integers in wich the user enters as many elements he wants and enter -99 (as a string) to exit the loop.Also valid entries range is 0 - 10 with 0 and 10 included.I get wrong results when printing the array tha it prints the last entry only and zeros after .The code is in Console C#.Any help will be appreciated.Thanks. 我的代码支持打印一个整数数组,用户输入他想要的多个元素并输入-99(作为字符串)退出循环。同样有效的条目范围是0 - 10,其中包含0和10。我错了打印数组时的结果,它只打印最后一个条目并在之后为零。代码在控制台C#中。任何帮助将不胜感激。谢谢。
namespace ExeNo1Ch7
{
class Program
{
static void Main(string[] args)
{
int numOfEntry;
int[] intNumbers = new int[100];
numOfEntry = GetArrayOfIntegers(intNumbers);
Console.WriteLine("\t\n You have entered " + numOfEntry + " values " + "\t\n" + "They are:");
for (int i = 0; i < numOfEntry; i++)
{
Console.WriteLine("\t\n" + intNumbers[i]);
}
Console.WriteLine("\t\n<< Press any key to Exit >> ");
Console.ReadKey();
}
public static int GetArrayOfIntegers(int[] anArray)
{
string strValue;
int counter = 0;
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
for (int i = 0; i < anArray.Length; i++)
{
while (strValue != "-99")
{
anArray[i] = int.Parse(strValue);
counter = counter + 1;
if (anArray[i] >= 0 && anArray[i] <= 10)
{
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
}
else
{
Console.WriteLine("\t\n Please try again entering an integer in the range (0 - 10) only,");
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
}
}
}
return counter;
}
You have this behavior because you're overwriting the same value over and over in your while
loop. 您有这种行为,因为您在while
循环中反复覆盖相同的值。
Basically, you have this : 基本上,你有这个:
for (i = 0; i < anArray.Length; i++)
{
while (strValue != "-99")
{
anArray[i] = int.Parse(strValue);
}
}
This means you will loop forever on the same i until the user inputs -99. 这意味着你将永远循环在同一个i上,直到用户输入-99。 You should drop the for loop and increment i in the while : 你应该删除for循环并在while中增加i:
public static int GetArrayOfIntegers(int[] anArray)
{
string strValue;
int counter = 0;
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
int i = 0;
while (strValue != "-99")
{
anArray[i] = int.Parse(strValue);
counter = counter + 1;
if (anArray[i] >= 0 && anArray[i] <= 10)
{
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
}
else
{
Console.WriteLine("\t\n Please try again entering an integer in the range (0 - 10) only,");
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
}
i++;
}
There's a second problem; 还有第二个问题; you assign the value before the range comparison. 您在范围比较之前指定值。 You should move the assignation and counter increment into the if
block, like this : 您应该将赋值和计数器增量移动到if
块中,如下所示:
public static int GetArrayOfIntegers(int[] anArray)
{
string strValue;
int counter = 0;
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
while (strValue != "-99")
{
int value = int.Parse(strValue);
if (value >= 0 && value <= 10)
{
anArray[counter] = value;
counter = counter + 1;
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
}
else
{
Console.WriteLine("\t\n Please try again entering an integer in the range (0 - 10) only,");
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
}
}
return counter;
}
What you have is a for
loop going through the input, but inside of that you also have a while
loop going through the input, which is very awkward, and it looks like it would result in you being able to enter that -99
number 100 times. 你所拥有的是一个for
循环通过输入,但是你也有一个while
循环通过输入,这是非常尴尬的,看起来它会导致你能够输入-99
数字100倍。
instead of using both a for
loop and a while
loop, just &&
the clauses together in the for loop 而不是同时使用for
循环和while
循环,只需将&&
的子句放在for循环中
for (int i = 0; i < anArray.Length && strValue != "-99"; i++)
{
you also should place a strValue = Console.ReadLine();
你还应该放置一个strValue = Console.ReadLine();
at the end of your for loop 在你的for循环结束时
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.