数组打印错误结果C#控制台
我的代码支持打印一个整数数组,用户可以在其中输入任意多的元素,然后输入-99(作为字符串)退出循环。有效的输入范围为0-10,包括0和10。我在打印数组时得到错误的结果,它只打印最后一个输入,然后打印0。代码在控制台C#中。如有任何帮助,将不胜感激。谢谢数组打印错误结果C#控制台,c#,arrays,C#,Arrays,我的代码支持打印一个整数数组,用户可以在其中输入任意多的元素,然后输入-99(作为字符串)退出循环。有效的输入范围为0-10,包括0和10。我在打印数组时得到错误的结果,它只打印最后一个输入,然后打印0。代码在控制台C#中。如有任何帮助,将不胜感激。谢谢 namespace ExeNo1Ch7 { class Program { static void Main(string[] args) { int numOfEntry; int[] intN
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;
}
名称空间ExeNo1Ch7
{
班级计划
{
静态void Main(字符串[]参数)
{
国际货币基金组织;
int[]intNumbers=新的int[100];
numOfEntry=getArrayOfFintegers(intNumbers);
Console.WriteLine(“\t\n您已输入“+numOfEntry+”值“+”\t\n“+”它们是:”);
for(int i=0;i”);
Console.ReadKey();
}
公共静态int getArrayFintegers(int[]anArray)
{
字符串标准值;
int计数器=0;
Console.Write(“\t\n输入一个从0到10的整数:”);
strValue=Console.ReadLine();
for(int i=0;i 如果(anArray[i]>=0&&anArray[i]你有一个for
循环通过输入,但是在这里面你也有一个,而循环通过输入,这是非常尴尬的,它看起来会导致你能够输入-99
数字100次
不要同时使用for
循环和while
循环,只需将&&
子句放在for循环中即可
for (int i = 0; i < anArray.Length && strValue != "-99"; i++)
{
for(int i=0;i
您还应该在for循环的末尾放置一个strValue=Console.ReadLine();
您有这种行为,因为您在while
循环中反复覆盖相同的值
基本上,你有:
for (i = 0; i < anArray.Length; i++)
{
while (strValue != "-99")
{
anArray[i] = int.Parse(strValue);
}
}
你的代码中有很多空行,阅读起来很糟糕。这是非常错误的。你的老师不会高兴的。回答你的问题,你只打印最后一项的原因是因为你根本没有增加i
——你所有的“旋转”在内部的while循环中完成,因此您只分配给第一个索引,当您最终输入-99
时,它执行剩余的99个步骤(不读取任何int)并返回。您不应该使用for循环和while循环。删除for循环并跟踪数组中已填充的条目(通过计数)使用计数来确定数组中存储用户输入的值。在你的代码工作之后,考虑把它放在代码审查中,实际上我的C级教师不分级任何作业,直到学期结束和学校管理。不做任何事。所以是的,我有点迷失了,这都归咎于T。he bad school sys:(但非常感谢您抓住我的天线:)如果您输入的数字超出了0-10范围(i
在不应该的情况下递增),则此操作将失败.EDIT:不,-99
被完全忽略,因此它不会被放入数组中。但是,是的,它将不必要地迭代for循环,并且只跳过除第一次之外的每次迭代。@Luaan您不会在for循环的签名中首先强制执行0-10
限制。@SamIam我不会使用for循环首先,它不适合读取未知的用户输入集。如果for循环没有意义,我不喜欢在其中包含多个条件。for is(语义)主要是一个迭代器,而不仅仅是一个语法稍有不同的while循环:PYou应该在验证检查中添加counter++
和i++
(if(anArray[i]>=0&&anArray[i]第二部分是错误的-现在你根本没有验证数字,每次都是零。必须在验证检查之前完成赋值。@Luaan Derp,你是对的。我会很快编辑它:P Fixx'd,看起来我在第二部分做得太快了,谢谢!这段代码工作得更好了!它解决了范围问题。非常感谢你的帮助:)
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++;
}
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;
}