官术网_书友最值得收藏!

Writing your first program in Visual Studio

VS is the IDE where developers mostly code while working with the C# language. As you already have a basic idea of how VS works, let's write our first program in VS. Let's create a console application, name the solution MyFirstApp, and press OK. The default solution template will be automatically added, which includes one Program.cs with the Main program, and a number of other files.

Let's build a program that generates an ATM machine. There will be a menu that has three options:

  • Withdraw
  • Deposit
  • Balance check

The withdrawal will be performed on the balance (initially $1,000) and a deposit will add an amount to the current balance. Now, let's see what the program looks like:

class Program
{
static void Main(string[] args)
{
int balance, depositAmt, withdrawAmt;
int choice = 0, pin = 0;
Console.WriteLine("Enter your ledger balance");
balance = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Your Pin Number ");
pin = int.Parse(Console.ReadLine());

if(pin != 1234)
{
Console.WriteLine("Invalid PIN");
Console.ReadKey(false);
return;
}

while (choice != 4)
{
Console.WriteLine("********Welcome to PACKT Payment Bank**************\n");
Console.WriteLine("1. Check Balance\n");
Console.WriteLine("2. Withdraw Cash\n");
Console.WriteLine("3. Deposit Cash\n");
Console.WriteLine("4. Quit\n");
Console.WriteLine("*********************************************\n\n");
Console.WriteLine("Enter your choice: ");
choice = int.Parse(Console.ReadLine());

switch (choice)
{
case 1:
Console.WriteLine("\n Your balance $ : {0} ", balance);
break;
case 2:
Console.WriteLine("\n Enter the amount you want to withdraw : ");
withdrawAmt = int.Parse(Console.ReadLine());
if (withdrawAmt % 100 != 0)
{
Console.WriteLine("\n Denominations present are 100, 500 and 2000. Your amount cannot be processed");
}
else if (withdrawAmt > balance)
{
Console.WriteLine("\n Sorry, insufficient balance.");
}
else
{
balance = balance - withdrawAmt;
Console.WriteLine("\n\n Your transaction is processed.");
Console.WriteLine("\n Current Balance is {0}", balance);
}
break;
case 3:
Console.WriteLine("\n Enter amount you want to deposit");
depositAmt = int.Parse(Console.ReadLine());
balance = balance + depositAmt;
Console.WriteLine("Your ledger balance is {0}", balance);
break;
case 4:
Console.WriteLine("\n Thank you for using the PACKT ATM.");
break;
}
}
Console.ReadLine();
}
}

Now, let's illustrate the program. The program requests a PIN number before opening the ATM machine. The PIN is not checked and can be anything. Once the program starts up, it creates a menu in the front of the console with all of the desired options.

You can see that the entire code is written inside a while loop, as it ensures that the program is kept alive for multiple executions. During execution, you can choose any of the options that are available and perform the action associated with it.

To execute the program, just click on the Run button on the toolbar of the IDE:

If the program does not run automatically, you can look at the Error List window to figure out the actual issue. If you made a mistake in the code, VS will show you the appropriate error message and you can double-click on this to navigate to the actual location.

主站蜘蛛池模板: 雅安市| 浙江省| 安丘市| 阜康市| 开江县| 高密市| 华宁县| 二连浩特市| 新巴尔虎右旗| 德清县| 扎囊县| 溧水县| 广东省| 屏边| 武义县| 南昌县| 正定县| 桦川县| 绩溪县| 宝山区| 吉林省| 虹口区| 无棣县| 当阳市| 汶上县| 太湖县| 安福县| 长海县| 辽阳县| 拉萨市| 宜宾县| 嘉鱼县| 建瓯市| 如东县| 依安县| 邳州市| 黔东| 榆社县| 时尚| 江孜县| 克什克腾旗|