0

This is my current code:

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;
            }

I'm getting the console to correctly identify that if v1 is greater than 50 or less than 10 it gives the message. But the code stops and doesn't continue asking for variables. I've tried following what is happening in the for loop and the if's loop but I'm just getting lost. I'm tasked with getting 5 variables by using loops and one console.readlin();. If that helps at all as to why I'm asking this question.

4

5 回答 5

2

Some error in your loop :

  1. for (int i = 0; i <= v5; i++) since v5 = 0, the loop will run only one time

  2. inputCheck = Convert.ToInt32(Console.ReadLine()); would only read the number once, and even if you fixed the loop to run 5 times, the input asked from the user will be just once, hence needed to move inputCheck = Convert.ToInt32(Console.ReadLine()); within the loop.

  3. The check for input for 1 to 50 can be improved in the loop, since u have the same restriction for all the variables.

Try this way :

    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;


            }
        }

    }
于 2013-02-19T02:04:22.657 回答
2

You need to put the Console.WriteLine("Please enter your name:"); and the userName = Console.ReadLine(); inside the beginning of the for loop if you want the input again.

于 2013-02-19T02:04:30.267 回答
2

Well, it looks like v5 is 0, so your loop is gonna run only once!

for (int i = 0; i <= v5; i++) //this will be true only once because v5 = 0
于 2013-02-19T02:05:34.553 回答
2

There are a few issues with your code. As others have pointed out,

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

Means you will never enter the for loop because i < v5 is false.

Secondly, it seems like you're trying to loop through v1 - v5 as variables. This is not the correct way to perform a loop over an arbitrary number of variables. You will need an array or some other type of collection.

Probably what you want is something like this:

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;
}
于 2013-02-19T02:08:30.390 回答
0
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;
        }

        ...
     }

The loop will have only first iteration. Because there is continue; in first iteration. v5 will be 0 always.i will be changed to 1 after first continue; and then i <= v5 - false (1 > 0 :) ), and loop will be completed.

于 2013-02-19T04:39:48.157 回答