2

I'm trying to make a number guessing game that prints only to the console so far, problem is my knowledge and problem solving is in its infancy and I wanted to up the program to guess between 1 and 1,000,000 but I can't figure out how to add the commas to the integers (Or if that's even allowed) so as to make reading them when printed to the console easier to read for the player.

int max;
int min;
int guess;
max = 1000000; 
min = 1;
guess = 500000;
Bassium
  • 25
  • 1
  • 6
  • commas are not allowed in integers. But, what I can't understand is what you want to achieve. Perhaps if you want what your output should look like we can help better, – Ehsan May 05 '15 at 01:07

2 Answers2

2

When you print the string, use a formatter:

int x = int.MaxValue;
Console.WriteLine(x.ToString("N"));

Prints:

2,147,483,647.00

See https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

Ron Beyer
  • 10,758
  • 1
  • 18
  • 35
2

Integer values are just numbers, they don't include formatting.

However, when you print them, you can format them. Example:

Console.WriteLine("You guessed {0:N0}.", guess);

With my regional setttings, prints:

500,000

See this overload of Console.WriteLine and here's the list of standard numeric formatting strings.

Blorgbeard
  • 97,316
  • 47
  • 222
  • 267