andrew speirs
andrew speirs

Reputation: 21

Create a list from a number entered by user

I've entered a number between 1 and 10, I now want to create a list to that size entered and store new integers to each element from more input.

so I enter 5 into the console, I now want to create a list of size 5, so that I can store 5 new integers into each element, here is my code so far -

I'm not looking for code to solve this, but a point in the right direction of what I need to learn to be able to do this,

thanks

using System;
using System.Collections.Generic;

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

            int totalOfMonsters = numberOfMonsters();
            Console.WriteLine("Total Number of Monsters = " + totalOfMonsters);

            Console.WriteLine();
            int numberOfHitPoints = HitPoints();//store this number into list - monstersInput?
            List<int> monstersInput = new List<int>(totalOfMonsters) ;

        }


        public static int numberOfMonsters()
        {
            string monsterNumbers;
            int min = 1;
            int max = 10;
            int result;
            do
            {
                Console.WriteLine("Enter a number between 1 and 10 for Number of Monsters.");
                monsterNumbers = Console.ReadLine();
                result = int.Parse(monsterNumbers);
                if (result < min || result > max) ;
                else
                    break;
            } while (true);

            return result;
        }

        public static int HitPoints()
        {
            // enter a number of hit points and store to list monsters
            int hitPoints;
            int min = 1;
            int max = 100;
            string hit;

            do
            {
                Console.WriteLine("Enter a Hit Number between 1 and 100 : ");
                hit = Console.ReadLine();
                hitPoints = int.Parse(hit);
                if (hitPoints < min || hitPoints > max) ;
                else
                    break;

            } while (true);

            return hitPoints;
        }
        //public static string Total()
        //{
        //    //final output to console with element list and hit points
        //    do
        //    {
        //        Console.WriteLine("Monster no.? has number of hit points");

        //    } while (true);

        //}


    }
}

Upvotes: 2

Views: 226

Answers (1)

aLittleSalty
aLittleSalty

Reputation: 315

Since you do know about loops, the best approach I can think of is:

  1. Ask for an integer from 1 to 10 and store it in a variable (totalOfMonsters)
  2. Create a list with size equal to totalOfMonsters (monstersInput)
  3. Create a loop that repeats itself "totalOfMonsters" times. 3.a Inside the loop ask on each iteration for the number of hitPoints and add it to the list.

You are already doing step 1 and 2:

static void Main(string[] args)
    {

        int totalOfMonsters = numberOfMonsters();
        Console.WriteLine("Total Number of Monsters = " + totalOfMonsters);

        Console.WriteLine();
        int numberOfHitPoints = HitPoints();//store this number into list - monstersInput?
        List<int> monstersInput = new List<int>(totalOfMonsters);
  }

But for step 3 you have to create a loop, you had the right idea with the line:

int numberOfHitPoints = HitPoints();

The only thing missing is the loop and saving each new element on the list. Since you don't want code solutions, let me give at least and outline on how you can do this:

    //Creating loop to fill the hitPoints of every monsters
    int someCounter = 1;
    do
    {
         //ask for the hitpoints 
         //save the hitpoints in the list
         //increase the value of the counter
    }
    while(someCounter <= sizeOfYourList);

Upvotes: 1

Related Questions