我的for循环中到底发生了什么

本文关键字:发生了 什么 for 循环 我的 | 更新日期: 2023-09-27 18:00:06

这是我当前的代码:

string userName = "";
        int v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0;
        int sum = 0;
        float avg;
        float variance;
        Console.WriteLine("Please enter your name:");
        userName = Console.ReadLine();
        Console.WriteLine("Please enter in a number between 10 and 50: ");
        int inputCheck = 0;
        inputCheck = Convert.ToInt32(Console.ReadLine());
        for (int i = 0; i <= v5; i++)
        {
            if (i == v1)
            {
                v1 = inputCheck;
                if (v1 < 10 || v1 > 50)
                {
                    Console.WriteLine("The number you have entered is invalid please enter a new variable: ");
                }
                continue;
            }
            if (i == v2)
            {
                v2 = inputCheck;
                if (v2 < 10 || v2 > 50)
                {
                    Console.WriteLine("The number you have entered is invalid please enter a new variable: ");
                }
                continue;
            }
            if (i == v3)
            {
                v3 = inputCheck;
                if (v3 < 10 || v3 > 50)
                {
                    Console.WriteLine("The number you have entered is invalid please enter a new variable: ");
                }
                continue;
            }
            if (i == v4)
            {
                v4 = inputCheck;
                if (v4 < 10 || v4 > 50)
                {
                    Console.WriteLine("The number you have entered is invalid please enter a new variable: ");
                }
                continue;
            }
            if (i == v5)
            {
                v5 = inputCheck;
                if (v5 < 10 || v5 > 50)
                {
                    Console.WriteLine("The number you have entered is invalid please enter a new variable: ");
                }
                continue;
            }

我正在让控制台正确地识别,如果v1大于50或小于10,它就会发出消息。但是代码停止了,不再继续请求变量。我试着跟踪for循环和if循环中发生的事情,但我只是迷路了。我的任务是通过使用循环和一个console.redlin();来获得5个变量;。如果这对我为什么要问这个问题有帮助的话。

我的for循环中到底发生了什么

您的代码存在一些问题。正如其他人所指出的,

int v5 = 0;
for (int = 0; i < v5; i++) ...

意味着您永远不会进入for循环,因为i < v5就是false

其次,您似乎正在尝试将v1-v5作为变量进行循环。这不是在任意数量的变量上执行循环的正确方法。您将需要一个数组或其他类型的集合。

也许你想要的是这样的东西:

string userName = "";
int[] v= new int[5];
int sum = 0;
float avg;
float variance;
Console.WriteLine("Please enter your name:");
userName = Console.ReadLine();
Console.WriteLine("Please enter in a number between 10 and 50: ");
for (int i = 0; i < v.Length; i++)
{
    int inputCheck = Convert.ToInt32(Console.ReadLine());
    while (inputCheck < 10 || inputCheck > 50)
    {
        Console.WriteLine("The number you have entered is invalid please enter a new variable: ");
        inputCheck = Convert.ToInt32(Console.ReadLine());
    }
    v[i] = inputCheck;
}

如果需要再次输入,则需要将Console.WriteLine("Please enter your name:");userName = Console.ReadLine();放在for循环的开头。

看起来v5是0,所以循环只运行一次!

for (int i = 0; i <= v5; i++) //this will be true only once because v5 = 0

循环中的一些错误:

  1. for (int i = 0; i <= v5; i++)由于v5=0,循环将只运行一次

  2. inputCheck = Convert.ToInt32(Console.ReadLine());只会读取一次数字,即使您将循环固定为运行5 times,用户要求的输入也只会一次,因此需要在循环中移动inputCheck = Convert.ToInt32(Console.ReadLine());

  3. 由于u对所有变量都有相同的限制,因此可以在循环中改进对1到50的输入的检查。

试试这个方法:

    string userName = "";
    int v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0;
    int sum = 0;
    float avg;
    float variance;
    Console.WriteLine("Please enter your name:");
    userName = Console.ReadLine();
    Console.WriteLine("Please enter in a number between 10 and 50: ");
    int inputCheck = 0;

    for (int i = 0; i < 5; i++)
    {
        inputCheck = Convert.ToInt32(Console.ReadLine());
        while (!(inputCheck < 10 || inputCheck > 50))
        {
             switch (i)
            {
                case 1:
                    v1 = inputCheck;
                    break;
                case 2:
                    v2 = inputCheck;
                    break;
                case 3:
                    v3 = inputCheck;
                    break;
                case 4:
                    v4 = inputCheck;
                    break;
                case 5:
                    v5 = inputCheck;
                    break;

            }
        }
    }
for (int i = 0; i <= v5; i++)
    {
        if (i == v1)
        {
            v1 = inputCheck;
            if (v1 < 10 || v1 > 50)
            {
                Console.WriteLine("The number you have entered is invalid please enter a new variable: ");
            }
            continue;
        }
        ...
     }

循环将只有第一次迭代。因为在第一次迭代中有CCD_ 13。v5将始终为0。在第一个continue;之后,i将变为1,然后i <= v5-false(1>0:)),循环将完成。