0

这个简单的程序输入一个整数并打印它们,但如果它看到 60 则停止打印

        string input = string.Empty;
        int intValue = 0;
        int[] numbers = new int[5];

        for (int i = 0; i < 4; i++)
        {
            input = Console.ReadLine();
            if (int.TryParse(input, out intValue))
                numbers[i] = intValue;
        }

        for (int i = 0; i < numbers.Length; i++)
        {
            while (numbers[i] != 60)
            {
                Console.WriteLine(intValue);
            }
        }

程序在第四个输入之后继续无限循环输入:1 2 3 4 4 4 4 4 4 ........等等

我不知道原因.... ^_^

4

2 回答 2

5
while (numbers[i] != 60)
{
    Console.WriteLine(intValue);
}

应该:

if (numbers[i] != 60)
{
    Console.WriteLine(intValue);
}
于 2012-11-23T17:12:36.857 回答
0

字符串输入 = string.Empty;

    int intValue = 0;
    int[] numbers = new int[5];

    for (int i = 0; i < 4; i++)
    {
        input = Console.ReadLine();
        if (int.TryParse(input, out intValue))
            numbers[i] = intValue;
    }

    for (int i = 0; i < numbers.Length; i++)
    {
        /*while (numbers[i] != 60)*/
        if (numbers[i] != 60)  // it should be if condition, while statement made it infinite
        {
            Console.WriteLine(intValue);
        }
    }
于 2012-11-24T20:34:22.580 回答