阵列打印错误结果C#Console

时间:2013-12-19 16:35:41

标签: c# arrays

我的代码支持打印一个整数数组,用户输入所需数量的元素并输入-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;
    }

2 个答案:

答案 0 :(得分:2)

您有这种行为,因为您在while循环中反复覆盖相同的值。

基本上,你有这个:

for (i = 0; i < anArray.Length; i++)
{
    while (strValue != "-99")
    {
        anArray[i] = int.Parse(strValue);
    }
}

这意味着你将永远循环在同一个i上,直到用户输入-99。您应该删除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++;
    }

还有第二个问题;您在范围比较之前指定值。您应该将赋值和计数器增量移动到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;
}

答案 1 :(得分:0)

你所拥有的是一个for循环通过输入,但在其中你还有一个while循环通过输入,这是非常尴尬的,看起来它会导致在你能够输入-99数字100次。

而不是同时使用for循环和while循环,只需将&&子句放在for循环中

    for (int i = 0; i < anArray.Length && strValue != "-99"; i++)
    {

您还应该在for循环结束时放置strValue = Console.ReadLine();