# Guess a number between 1-100

• ### Question

• Hello im very new in coding and just started practising C#. I want to do Guess number game and put the answers on list and print them. I got the game working but im lost how to put the guesses on integerList and print them too.

 { class Program { static void Main(string[] args) { List integerList = new List(); Random rnd = new Random(); int guess = 0; int num = rnd.Next(1, 100) Console.WriteLine("Guess a number between 1-100"); int i = 0; while(guess != num) { try { guess = Convert.ToInt32(Console.ReadLine()); if (guess > num) { Console.WriteLine("Too High"); } else { Console.WriteLine("Too Low"); } } catch { Console.WriteLine("Guess must be a number"); i--; } i++; } Console.WriteLine("Congrats, it took you " + i + " tries"); Console.ReadLine(); } } }

• Edited by Tuesday, February 6, 2018 11:19 AM
Tuesday, February 6, 2018 11:13 AM

• Try the following, note I made some changes for ensuring we have a number along with adding the guesses to a list.

```using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<int> guessList = new List<int>();
Random random = new Random();

int returnValue = random.Next(1, 100);

int Guess = 0;

Console.WriteLine("I am thinking of a number between 1-100.  Can you guess what it is?");

while (Guess != returnValue)
{

if (!int.TryParse(Console.ReadLine(), out Guess))
{
Console.WriteLine("Invalid input");
continue;
}
else
{
}

if (Guess < returnValue)
{
Console.WriteLine(\$"No, the number I am thinking of is higher than {Guess}." +
" Can you guess what it is?");
}
else if (Guess > returnValue)
{
Console.WriteLine(\$"No, the number I am thinking of is lower than {Guess}." +
" Can you guess what it is?");
}

}

Console.WriteLine(\$"Well done! The answer was {returnValue}");
Console.WriteLine("Guesses where");
Console.WriteLine(string.Join("\n",guessList.ToArray()));
}
}
}
```

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

• Marked as answer by Tuesday, February 6, 2018 4:23 PM
Tuesday, February 6, 2018 12:17 PM

### All replies

• Hello,

For something to go by see this post.

Code from post.

```static void Main(string[] args)

{

Random random = new Random();

int returnValue = random.Next(1, 100);

int Guess = 0;

Console.WriteLine("I am thinking of a number between 1-100.  Can you guess what it is?");

while (Guess != returnValue)
{

if (Guess < returnValue)
{
Console.WriteLine("No, the number I am thinking of is higher than " + Guess + ". Can you guess what it is?");
}
else if (Guess > returnValue)
{
Console.WriteLine("No, the number I am thinking of is lower than " + Guess + ". Can you guess what it is?");
}

}

Console.WriteLine("Well done! The answer was " + returnValue);

}```

Note, in the future place your code into a code block.

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

Tuesday, February 6, 2018 11:25 AM
• My game is working but i want to add the guesses on int list called "arvausLista" and print the guesses. Im lost :D

```  static void Main(string[] args)
{
Random rnd = new Random();
int Arvaus = 0;
int Vastaus = rnd.Next(1, 100);
int i = 0;
List<int> arvausLista = new List<int>();

Console.WriteLine("Arvaa numero 1-100 välillä");

while (Arvaus != Vastaus)
{

try
{

if (Arvaus > Vastaus)
{
Console.WriteLine("Luku on pienempi kuin: " + Arvaus);

}

else
{
Console.WriteLine("Luku on suurempi kuin: " + Arvaus);
}
}

catch
{
Console.WriteLine("Arvauksen on oltava numero!");
i--;
}

i++;

}
Console.WriteLine("Onnittelut! Vastaus oli: " + Vastaus);
Console.WriteLine("Sinulla meni " + i + " yritystä.");

}

}
}

```

Tuesday, February 6, 2018 11:56 AM
• So your problem is you do not know how to add an item to a List?

```List<int> GuessList = new List<int>();

```

P.S. Just one general tip on programming, use English for everything in your program except perhaps output texts.
it makes it a lot easier if you ever have to share the code with someone else, that does not understand your language.

Tuesday, February 6, 2018 12:06 PM
• Try the following, note I made some changes for ensuring we have a number along with adding the guesses to a list.

```using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<int> guessList = new List<int>();
Random random = new Random();

int returnValue = random.Next(1, 100);

int Guess = 0;

Console.WriteLine("I am thinking of a number between 1-100.  Can you guess what it is?");

while (Guess != returnValue)
{

if (!int.TryParse(Console.ReadLine(), out Guess))
{
Console.WriteLine("Invalid input");
continue;
}
else
{
}

if (Guess < returnValue)
{
Console.WriteLine(\$"No, the number I am thinking of is higher than {Guess}." +
" Can you guess what it is?");
}
else if (Guess > returnValue)
{
Console.WriteLine(\$"No, the number I am thinking of is lower than {Guess}." +
" Can you guess what it is?");
}

}

Console.WriteLine(\$"Well done! The answer was {returnValue}");
Console.WriteLine("Guesses where");
Console.WriteLine(string.Join("\n",guessList.ToArray()));