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

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.

主站蜘蛛池模板: 裕民县| 宜宾市| 盱眙县| 天柱县| 仁怀市| 水城县| 苏州市| 留坝县| 天全县| 开封县| 巫山县| 南宫市| 安康市| 江都市| 会泽县| 延津县| 锡林郭勒盟| 大连市| 天全县| 黎城县| 北流市| 肥乡县| 孟村| 永州市| 阳江市| 南郑县| 航空| 井冈山市| 那曲县| 南昌县| 安仁县| 渭源县| 依安县| 贵港市| 西城区| 巴林左旗| 蓬莱市| 麦盖提县| 萨嘎县| 雷波县| 都匀市|