Adam
Adam

Reputation: 3

Check if input matches in list

I have used two list, and I am not sure what method to use to print the input. Basically i want to show all the months first, then let the user choose one month and show the days.

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Choose a month: ");
            Console.ReadLine();

            List<int> list = new List<int> { 31, 28, 31, 30, 31, 30, 31, 31, 30, 30, 30, 31 };
            List<string> Manad = new List<string> { "Januari", "Februari", "Mars", "April", "Maj", "Juni", "Juli", "Augusti", "September", "Oktober", "November", "December" };

            string inmat;
            inmat = Console.ReadLine();

            if (inmat == 31) ;
            {
                Console.WriteLine(Manad[0] + " " + list[0]);
                Console.ReadLine();
            }
        } 
    }
}

Upvotes: 0

Views: 559

Answers (2)

jdweng
jdweng

Reputation: 34421

With two lists it is better to use a dictionary like the code below

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

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Choose a month: ");
            Console.ReadLine();

            Dictionary<string,int> dict = new Dictionary<string,int>(){
                {"Januari", 31},  
                {"Februari", 28},
                {"Mars", 31},
                {"April", 30},
                {"Maj", 31},
                {"Juni", 30},
                {"Juli", 31},
                {"Augusti", 31},
                {"September", 30},
                {"Oktober", 31},
                {"November", 30},
                {"December", 31}
            };

            int nummberOfDays = dict["Maj"];

            int inmat = int.Parse(Console.ReadLine());
            //get all the months with days = inmat
            Console.WriteLine(string.Join(",", dict.AsEnumerable().Where(x => x.Value == inmat).Select(y => y.Key).ToArray()));
            Console.ReadLine();

        }


    }

}
​

Upvotes: 1

Noctis
Noctis

Reputation: 11763

without spoiling the fun and doing your work for you, you want to have a look at foreach.

List<string> demo_list = new List<String> {"hello", "world", "!"};

foreach (var s in demo_list)
{
    Console.WriteLine(s);
}

Upvotes: 0

Related Questions