L
Learning to code
Hello,
I'm am learning to program and hoping someone could help me with my code question.
Below I have pasted the C# dictionary code example I am working on. The intent of the program is to ask a user a question, read their response, and respond back to the user. This is a counsel application written in C#, usingthe visual studio 2010 express edition.
Reviewing the code below, where I am stuck is on this line:
Console.WriteLine("my response is {0}",dict[myInput]);
What I would like the program to do is read the response the user entered, find the key within the dictionary, and respond back to the user based on the value for that key.
I believe what is incorrect above is where I am trying to pass in the string value from the myInput variable, into the dictionary.
If the program worked correctly - it would ask the user their name, if the user typed "tara", it would respond back "Hi Tara!" (based on the string value in the dictionary).
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Example
{
public static void Main()
{
// Create and initilize dictionary of strings.
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Tara", "Hi Tara!");
dict.Add("Ryan", "Hi Ryan!");
//Introduction
Console.WriteLine("What is your name?");
//Read Answer
String myInput = Console.ReadLine();
//Respond
Console.WriteLine("my response is {0}",dict[myInput]);
//Stop
Console.ReadLine();
}
}
}
I'm am learning to program and hoping someone could help me with my code question.
Below I have pasted the C# dictionary code example I am working on. The intent of the program is to ask a user a question, read their response, and respond back to the user. This is a counsel application written in C#, usingthe visual studio 2010 express edition.
Reviewing the code below, where I am stuck is on this line:
Console.WriteLine("my response is {0}",dict[myInput]);
What I would like the program to do is read the response the user entered, find the key within the dictionary, and respond back to the user based on the value for that key.
I believe what is incorrect above is where I am trying to pass in the string value from the myInput variable, into the dictionary.
If the program worked correctly - it would ask the user their name, if the user typed "tara", it would respond back "Hi Tara!" (based on the string value in the dictionary).
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Example
{
public static void Main()
{
// Create and initilize dictionary of strings.
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Tara", "Hi Tara!");
dict.Add("Ryan", "Hi Ryan!");
//Introduction
Console.WriteLine("What is your name?");
//Read Answer
String myInput = Console.ReadLine();
//Respond
Console.WriteLine("my response is {0}",dict[myInput]);
//Stop
Console.ReadLine();
}
}
}