Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to create a loop that only last until the right input is given by the user.
Requriments are:
The number should be between 1 t0 100 and the number entered should not be same as previous.

What I have tried:

C#
for (int i = 0; i < Number.Length; i++)
{
Console.WriteLine("Enter number 1. The number must be between 1-100");
Number[i] = Int32.Parse(Console.ReadLine());

    if (Number[i] < 1)
    {
        Console.WriteLine("Please reenter number. The number must be between 1-100");
    }

    else if (Number[i] > 100)
    {
        Console.WriteLine("Please reenter number 1. The number must be between 1-100");
    }
    else
    {
       //store the value in the array
        Number[i] = Number[i];
    }


}

for (int i = 0; i < Number.Length; i++)
{

    Console.WriteLine("Enter number 2. The number must be between 1-100");
    Number[i] = Int32.Parse(Console.ReadLine());

    if (Number[i] < 1)
    {
        Console.WriteLine("Number less than 1. Please reenter number 2.
The number must be between 100");
    }
    else if (Number[i] > 100)
    {
        Console.WriteLine("Please reenter number 2. The number must be between 1-100");
    }
    else if (Number[i] == Number[i])
    {
        Console.WriteLine("Numbers must be unique. It cannot match any of the pervisouly entered numbers");
    }
    else
    {
        Number[i] = Number[i];
    }
}

for (int i = 0; i < Number.Length; i++)
{
    Console.WriteLine("Enter number 3. The number must be between 1-100");
    Number[i] = Int32.Parse(Console.ReadLine());

    if (Number[i] < 1)
    {
        Console.WriteLine("Please reenter number 3. The number must be between 1-100");
    }
    else if (Number[i] > 100)
    {
        Console.WriteLine("Please reenter number 3. The number must be between 1-100");
    }
    else if (Number[i] == Number[i])
    {
        Console.WriteLine("Numbers must be unique. It cannot match any of the pervisouly entered numbers");
    }
    else
    {
        Number[i] = Number[i];
    }
Posted
Updated 19-Apr-17 21:30pm
v2
Comments
Bhola Ram Sahu 19-Apr-17 20:44pm    
||else if (Number[i] == Number[i])|| This condition will always true. Thats why you are not getting correct result.

Use inner loop or a function to check duplicate value.
[no name] 19-Apr-17 21:04pm    
And your question is what?
What happens when the user inputs "A"?

1 solution

Your logic is overly complicated. You can do all in one loop. Also you need to learn to encapsulate logic into methods/functions/classes in order to reuse code and make it more readable.
C#
for (int i = 0; i < Number.Length; i++)
{
    while (true) // loop until you have correct number
    {
        var number = ReadNumber();
        if (IsValid(number))
        {
            Number[i] = number; // store if it's correct
            break; // exit the inner while-loop as we have correct number
        }
    }
}

int ReadNumber()
{
    Console.WriteLine("Enter number 1. The number must be between 1-100");
    return Int32.Parse(Console.ReadLine());
}

bool IsValid(int number)
{
    if (number < 1)
    {
        Console.WriteLine("Please reenter number. The number must be between 1-100");
        return false;
    }
    if (number > 100)
    {
        Console.WriteLine("Please reenter number. The number must be between 1-100");
        return false;
    }
    return true;
}

I didn't run this through compiler so there may be typos. I also didn't include uniqueness check on purpose. Try to figure that on your own as this is another exercise in encapsulation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900