Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made a program asking me to input 10 different numbers ranging from 10 to 100 and the numbers can't repeat. I can make it so the program doesn't allow numbers below 10 or greater than 100, but I can't make it so they don't repeat without making a very long code.

What I have tried:

I do the "for....else" statement to keep the numbers in the 10 - 100 range. I used it to make sure the numbers don't repeat also. It gets very lengthy the closer I get to inputting the tenth number. The program works, but it is too much to type in.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp9
{
    class Program
    {
        static void Main(string[] args)
        {
            string userNumber;
            string previousNumber = ("");
            int numberint;

            Console.WriteLine("I need you to enter 10 different numbers ranging from 10 to 100");

            for (int number = 0; number < 10; number++)
                
            {
                Console.Write("Please enter your number: ");
                userNumber = Console.ReadLine();
                numberint = int.Parse(userNumber);
                if (numberint > 100 || numberint < 10 || userNumber == previousNumber) 
                {
                    Console.WriteLine("Invalid number. Please enter a number ranging from 10 to 100");
                    number--;
                }
                else
                    Console.WriteLine("Thank you for entering " + userNumber);
            }
Posted
Updated 2-Dec-17 4:16am
v2
Comments
Patrice T 1-Dec-17 22:17pm    
And you plan to show your code?
Richard MacCutchan 2-Dec-17 8:58am    
Sounds like you are doing it wrong. But since we cannot see your code we have no idea exactly what you are doing.

You might use a HashSet[^] for the purpose, e.g.
HashSet<int> number = new HashSet<int>();
while (number.Count < 10)
{
  int candidate;
  Console.WriteLine("Please, enter a number between 10 and 100");
  if (int.TryParse(Console.ReadLine(), out candidate))
  {
    if (candidate >= 10 && candidate <= 100)
      number.Add(candidate); // adding a duplicate to the HashSet has no effect
  }
}
// 'number' contains 10 different integers in the 10..100 range. Showing them:
foreach (int n in number)
  Console.WriteLine(n);
 
Share this answer
 
v2
This is just an example algorithm in pseudo code to solve your problem:
0. Initialize an array container
1. Get a user input 
2. Converted that input to a number
3. IF (that number is between 10 and 100 AND is not already in the array container) THEN
   3.1  Add it to that array container
4. Go to 1 if that array container contains less than 10 elements.
You can use any programming language to implement this algorithm. For specific language syntaxes and features, ask Google.
 
Share this answer
 
v2

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