In this lesson, you'll put your newfound C# skills to the test by building a simple console application. This application will take input from the user, perform a calculation based on that input, and then display the result back to the user. This hands-on experience will solidify your understanding of variables, data types, operators, and control flow, all while giving you a tangible project to showcase your progress. This task directly applies the concepts you've learned in the previous lessons, reinforcing your understanding and preparing you for more complex projects in the future.
The goal is to create a console application that asks the user for two numbers, adds them together, and displays the sum. Here's a step-by-step guide:
Console.WriteLine() to display the prompts and Console.ReadLine() to read the user's input. Since Console.ReadLine() returns a string, you'll need to convert the input to a numerical data type (like int or double) using int.Parse() or double.Parse().+ operator. Store the result in a variable.Console.WriteLine() to display the result to the user. You can use string concatenation or string interpolation to create a user-friendly message.Here's the C# code that implements the calculator application:
using System;
namespace CalculatorApp
{
class Program
{
static void Main(string[] args)
{
// Prompt the user for the first number
Console.Write("Enter the first number: ");
string? input1 = Console.ReadLine(); // Read the first number as a string
// Check if the input is null before parsing
if (string.IsNullOrEmpty(input1))
{
Console.WriteLine("Invalid input. Please enter a number.");
return; // Exit the program if the input is invalid
}
// Convert the first number to a double
double num1;
if (!double.TryParse(input1, out num1))
{
Console.WriteLine("Invalid input. Please enter a valid number.");
return; // Exit the program if the input is invalid
}
// Prompt the user for the second number
Console.Write("Enter the second number: ");
string? input2 = Console.ReadLine(); // Read the second number as a string
// Check if the input is null before parsing
if (string.IsNullOrEmpty(input2))
{
Console.WriteLine("Invalid input. Please enter a number.");
return; // Exit the program if the input is invalid
}
// Convert the second number to a double
double num2;
if (!double.TryParse(input2, out num2))
{
Console.WriteLine("Invalid input. Please enter a valid number.");
return; // Exit the program if the input is invalid
}
// Calculate the sum
double sum = num1 + num2;
// Display the result using string interpolation
Console.WriteLine($"The sum of {num1} and {num2} is: {sum}");
// Keep the console window open until a key is pressed
Console.ReadKey();
}
}
}
using System;: This line imports the System namespace, which contains essential classes like Console.namespace CalculatorApp: This declares a namespace for your application, helping to organize your code.class Program: This defines the main class of your application.static void Main(string[] args): This is the main method where the program execution begins.Console.Write("Enter the first number: ");: This line displays a message to the user, prompting them to enter the first number. Console.Write keeps the cursor on the same line after printing the message.string? input1 = Console.ReadLine();: This line reads the user's input from the console and stores it as a string in the input1 variable. The ? after string indicates that the variable can be null.if (string.IsNullOrEmpty(input1)): This line checks if the input string is null or empty. This is important to prevent errors when parsing the input.double num1;: This line declares a variable num1 of type double to store the first number.if (!double.TryParse(input1, out num1)): This line attempts to convert the input string to a double using double.TryParse(). TryParse() is a safer alternative to double.Parse() because it doesn't throw an exception if the conversion fails. Instead, it returns a boolean value indicating whether the conversion was successful. The out keyword is used to pass the converted value to the num1 variable.