Ask for Favorite Food and Print a Sentence
Table of Contents
- Ask for Favorite Food and Print a Sentence
- Introduction
- Learning Objectives
- Step-by-Step Tutorial
- Step 1: Set Up Your Project
- Step 2: Writing the Source Code
- Step 3: Example Output
- Code Explanation
- Summary
- Multiple-Choice Questions (Bloom’s Taxonomy)
- Exercises, Assessment, and Lab Exam
- Introduction
- Exercises
- Assessment
- Lab Exam
Introduction
One of the most fundamental skills in programming is learning how to interact with users by accepting input and displaying output. Input allows the user to provide data to the program, while output lets the program respond or give feedback. In C#, the Console.ReadLine()
method is the most common way to receive input from the keyboard.
In this lesson, we will create a fun and simple program where the user will enter their favorite food, and the program will respond with a sentence using their input. This exercise will help you understand how Console.ReadLine()
works, how to store user-provided information in variables, and how to display dynamic messages back to the user.
While this task seems simple, it introduces essential programming concepts such as:
- Variables and data types
- User interaction through input and output
- String concatenation and interpolation
By mastering these concepts, you’ll be ready to create more interactive and meaningful console applications.
Learning Objectives
Introduction to Objectives:
The goal of this lesson is to help you develop a solid foundation in handling input and output operations. Using Outcome-Based Education (OBE), these objectives are categorized into four levels to guide your learning journey.
- Understand – Explain how
Console.ReadLine()
works and why user input is important in programming. - Learn – Identify and demonstrate proper use of variables and string handling when working with user input.
- Practice – Write a program that asks for the user’s favorite food and displays a personalized sentence using their response.
- Apply – Enhance the program by adding validation and more interactive features.
Step-by-Step Tutorial
Step 1: Set Up Your Project
- Open Visual Studio or Visual Studio Code.
- Create a new Console Application project.
- Name your project:
FavoriteFoodProgram
.
Step 2: Writing the Source Code
Below is the beginner-friendly C# source code for this lesson:
using System;
namespace FavoriteFoodProgram
{
class Program
{
static void Main(string[] args)
{
// Display a welcome message
Console.WriteLine("Welcome to the Favorite Food Program!");
// Ask the user to input their favorite food
Console.Write("Please enter your favorite food: ");
string favoriteFood = Console.ReadLine(); // Capture input from the user
// Display a message using the user's input
Console.WriteLine("Wow! " + favoriteFood + " sounds delicious. Enjoy your meal!");
// End program message
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
}
}
}
Step 3: Example Output
Input:
Welcome to the Favorite Food Program!
Please enter your favorite food: Pizza
Output:
Wow! Pizza sounds delicious. Enjoy your meal!
Press Enter to exit...
Code Explanation
Console.WriteLine()
– Prints a message or output to the screen.Console.ReadLine()
– Captures the user’s keyboard input as a string.- Variable
favoriteFood
– Stores the user’s input for later use. - String concatenation (
+
) – Combines text and variables into one message.
This simple program introduces string handling and how to make programs interactive and dynamic.
Summary
In this lesson, you learned how to create a simple program that interacts with the user by asking for their favorite food and printing a personalized response. We explored how to use Console.ReadLine()
to capture input, how to store data in variables, and how to display meaningful output. These foundational skills are essential for building interactive applications and serve as the basis for more complex programming tasks.
Multiple-Choice Questions (Bloom’s Taxonomy)
- Remembering:
Which method is used to capture user input in C#?
A.Console.WriteLine()
B.Console.Input()
C.Console.ReadLine()
D.Console.Scan()
Answer: C
- Understanding:
Why do we store the value ofConsole.ReadLine()
in a variable likefavoriteFood
?
A. To delete it later
B. To use it multiple times in the program
C. To convert it automatically to a number
D. It is optional and not necessary
Answer: B
- Applying:
If the user types “Burger,” what will the program output?
A. Burger sounds delicious.
B. Wow! Burger sounds delicious. Enjoy your meal!
C. Favorite food is Burger.
D. Burger is stored successfully.
Answer: B
- Analyzing:
What would happen if you do not useConsole.ReadLine()
at all in the program?
A. The program will not run.
B. The program will not accept user input.
C. The output will show random data.
D. It will automatically guess the input.
Answer: B
- Evaluating:
Which is the best way to improve the program’s interaction with the user?
A. Add input validation and handle empty responses.
B. Remove allConsole.WriteLine()
statements.
C. Limit the user to specific food options only.
D. Ignore invalid or blank inputs.
Answer: A
Exercises, Assessment, and Lab Exam
Introduction
Practicing beyond the provided example will help you develop problem-solving skills and strengthen your understanding of user input and output handling. The following tasks gradually increase in difficulty to ensure you gain confidence and mastery of the topic.
Exercises
- Modify the program to:
- Ask the user for two favorite foods and print a combined message.
- Example: “Pizza and Pasta are great choices!”
- Update the program to:
- Display a thank-you message that includes both the current date and time.
- Use string interpolation instead of concatenation:
Console.WriteLine($"Wow! {favoriteFood} sounds delicious.");
Assessment
Create a console program that:
- Asks for the user’s name and favorite food.
- Prints a message like:
“Hello Sarah! It’s great to know that you love Sushi.” - Add a validation step to ensure the user does not leave the input blank.
- If the input is blank, display an error message and ask again.
Assessment Criteria:
- Correct use of
Console.ReadLine()
and string variables. - Implementation of validation logic.
- Clear and professional output formatting.
Lab Exam
Build a Simple Food Ordering Console Application:
- Ask for the customer’s name.
- Provide a list of 3 food items with numbers (e.g., 1. Pizza, 2. Burger, 3. Salad).
- Allow the user to select their order by entering a number.
- Display a confirmation message showing the user’s choice.
- Include basic input validation to handle incorrect selections.
Additional Requirements:
- Use comments to explain your code.
- Implement at least one
if-else
orswitch
statement. - Ensure the program handles errors gracefully.
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.