judgementkazzzy
judgementkazzzy

Reputation: 3

how to output the contents of the list

essentially I'm making a guessing game for an assignment but as I try to output the list it comes out as

System.Collections.Generic.List`1[System.Int32]

System.Collections.Generic.List`1[System.Int32]

essentially I just need to store a users guess number and their attempt number so that once they guess the correct number it will display it as

"YOU WON, the number was ___ and here are your attempts

  1. you chose 45
  2. you chose 54
  3. you chose 32
  4. you chose ___
using System;
using System.Collections.Generic;

namespace main__4_8_2021_
{
    class Program
    {
        public static void Main(string[] args)
        {

            while (true)
                try
                {
                    int NumberOfTries = 0;
                    Console.WriteLine("Guess a number between 1 and 100");
                    int number = Convert.ToInt32(Console.ReadLine());
                    List<int> mylist2 = new List<int>(number);
                    List<int> mylist = new List<int>(NumberOfTries);

                    int rng = new Random().Next(1, 101);

                    if (number == rng)
                        {
                            Console.WriteLine("Your guess was correct! The number was " + number + "!");
                            Console.WriteLine(mylist);
                            Console.WriteLine(mylist2);
                            break;
                        }
                        else if (number > rng)
                        {
                            NumberOfTries++;
                            Console.WriteLine("Your guess was too high ");
                            Console.WriteLine(mylist);
                            Console.WriteLine(mylist2);
                            Console.WriteLine("you now have done " + NumberOfTries + " Tries");
                        }
                        else if (number < rng)
                        {
                            NumberOfTries++;
                            Console.WriteLine("too low, ");
                            Console.WriteLine(mylist);
                            Console.WriteLine(mylist2);
                            Console.WriteLine("you now have done " + NumberOfTries + " Tries");
                        }

                        Console.Write($"Try again. ");
                }

                catch(Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
    }

(the other console.writelines of the lists are just there for debug)

Upvotes: 0

Views: 88

Answers (2)

Mat
Mat

Reputation: 2072

Console.WriteLine(mylist);

If value is null, only the line terminator is written. Otherwise, the ToString method of value is called to produce its string representation, and the resulting string is written to the standard output stream.

SOURCE https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-5.0#System_Console_WriteLine_System_Object_

this means that you actually calling the ToString method of the list. Most of the "complex" types do not overwrite the ToString method which results in just returning the string representation of Type. In your case: List<int> which is represented as System.Collections.Generic.List`1[System.Int32]

you may also want to have a look at https://learn.microsoft.com/en-us/dotnet/api/system.object.tostring?view=net-5.0

Upvotes: 0

zaitsman
zaitsman

Reputation: 9519

You have a number of issues with your code.

First, you don't need a counter for number of attempts, a Count property on the List is enough.

Second, you need to keep that list outside of the loop so it is not recreated each time.

Try the below

using System;
using System.Collections.Generic;

namespace main__4_8_2021_
{
    class Program
    {
        public static void Main(string[] args)
        {
            List<int> mylist = new List<int>();
            void PrintListContents() {
                   Console.WriteLine("here are your attempts");
                   var index = 0;
                   foreach(var value in mylist) {
                       Console.WriteLine($"{index}. You chose {value}");
                       index++;
                   }
            }


            while (true)
                try
                {
                    Console.WriteLine("Guess a number between 1 and 100");
                    int number = Convert.ToInt32(Console.ReadLine());
                    mylist.Add(number);
                    int rng = new Random().Next(1, 101);

                    if (number == rng)
                        {
                            Console.WriteLine("Your guess was correct! The number was " + number + "!");
                            PrintListContents()
                            break;
                        }
                        else if (number > rng)
                        {
                            NumberOfTries++;
                            Console.WriteLine("Your guess was too high ");
                            PrintListContents()                            
                            Console.WriteLine("you now have done " + myList.Count + " Tries");
                        }
                        else if (number < rng)
                        {
                            NumberOfTries++;
                            Console.WriteLine("too low, ");
                            PrintListContents()                            
                            Console.WriteLine("you now have done " + myList.Count + " Tries");
                        }

                        Console.Write($"Try again. ");
                }
                catch(Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
    }

Upvotes: 1

Related Questions