C# Lesson 2

Learn how to read input from the user in the second lesson from our series on learning C# for beginners.

In Lesson 2 we will be going over basic IO. IO stands for Input Output. We'll be writing text to the console and reading input from the user.
Afterwards you can continue to the next lesson!
Or go back and review the previous lesson.
We also have the source code on GitHub.


using System;

namespace Lesson2
{
    class ConsoleIO
    {
        static void Main(string[] args)
        {
            string name;
            Console.WriteLine("What is your name?");
            name = Console.ReadLine();

            int age;
            Console.WriteLine("How old are you?");
            age = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Your name is " + name + " and you are " + age + " years old.");

            double value;
            Console.WriteLine("What is the decimal value of 1/10?");
            value = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("I hope " + value + " is right!");

        }
    }
}

/*
* TASK:
* Create a program that fills out a mad lib with data supplied by the user.
* (Hint: a Verb is an action. A noun is a person/place/thing.
* An adjective describes a person/place/thing.)
* 
* NOTE: Be sure that you have proper spaceing between words.
* 
* Example Output:
*   Enter Occupation(a job): Gardener
*   Enter Noun1: Bird
*   Enter Adjective1: Blue
*   Enter Noun2: Hat
*   Enter Verb1: Fight
*   Enter Adjective2: Happy
*   Enter Noun3: Boots
*   Enter Verb2: Run
*   Enter Noun4: Gloves
*   Enter Verb3: Fall
*   
*   Today a <Occupation> named <Noun4> came to our school to talk to us about her job.
*   She said the most important skill you need to know to do her job is to be able
*   to <Verb2> around (a) <Adjective1> <Noun3>.
*   She said it was easy for her to learn her job because she loved to <Verb1> when she was my age--
*   and that helps a lot! If you're considering her profession,
*   I hope you can be near (a) <Adjective2> <Noun1>. That's very important!
*   If you get too distracted in that situation you won't be able to <Verb3> next to (a) <Noun2>!
*   
*********************
* Credit to Steve Hanson https://www.glowwordbooks.com/blog/2016/04/19/online-kids-mad-libs-about-jobs/
* 
*/

using System;

namespace Lesson2Task
{
    class ConsoleIOTask
    {
        static void Main(string[] args)
        {
            //You will need to make 10 string variables.    ex: string occupation;
            //Then prompt the user to enter a word.         ex: Console.WriteLine("Enter Occupation(a job): ");
            //Set the variable equal to the user's input.   ex: occupation = Console.ReadLine();
            //Finally, after you have all 10 words.
            //You can print the madlib                      ex: Console.WriteLiine("Today a " + occupation + " named");
        }
    }
}

Recommended posts

We have similar articles. Keep reading!

C# Lesson 7

You're familiar with "while" loops in C#, but but it's time to learn about "for" loops. Let's get loopy in the 7th lesson in our series about learning C#.

C# Lesson 10

Lesson 10 in a our series about learning C# teaches you about working with arrays.

C# Lesson 4

You truly need this lesson from our beginner series if you aren't familiar with using a Boolean in C#.

Bubble Sort

Sample source code for implementing bubble sort in C# with an accompanying video explaining how to do it yourself.

Comments

Log in or sign up to leave a comment.