Console Calculator Programming C #

Experiment 1: Programming C # Console Calculator

Today is 2020.03.01! First, make a wish, I wish March treat, you must be happy ah! Keke Well, well - trained quickly knock bar code!


Requirements Review


[Requirements] experiment

For two users input from the keyboard, a user specified operation and outputs the operation result.

【Purpose】

  1. Understand the characteristics of the .NET Framework.

  2. Familiar with Visual Studio 2015 development environment.

  3. Learn to write a console application in C #.

  4. Master the basic input and output method in the console application.

[Type of experiment]

Confirmatory.

[Experimental] content

(Prompting the user) enter the first digit: 20 (here, examples of user input)

(Prompting the user) enter the second number: 45 (here, examples of user input)

(Prompting the user) input of the operational sign: + (here examples of user input)

Result of the program: 65

(Prompt the user) whether to continue operations? Is (Y)? No (N)? (Selected by the user)

Select "Yes" means to jump after running a successful start a new operation for the next. "No" the end of the operation. To achieve the cycle with a goto statement, while statement, do statement and so on.

Static Methods class Console.WriteLine () provide cues;

Static Methods class Convert.ToDouble (Console.ReadLine ()) receiving a digital input by the user.

Can be plus (+), subtract (-), multiply (*), any addition (/) for calculating the four operations.

Inside the block with the jump goto statement, to achieve the cycle.

, The corresponding switch configuration is calculated to achieve a multi-branch structure according to a user's selection

And household users pay user input to be considered illegal situation. Use try ... catch ... finally structure can capture user input error.
**

CSharp code implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3计算器
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("实验1:控制台计算器程序设计,by SDNU咸鱼小十七酱ovo");
            double firstNumber = 0;
            double secondNumber = 0;
        input:
            try
            {
                Console.WriteLine("请输入第一个数:");
                firstNumber = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入第二个数:");
                secondNumber = Convert.ToDouble(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("输入错误!请再次输入:\n");
                goto input;
            }
            double result;
            Console.WriteLine("请从下方选择您需要进行的运算:");
            Console.WriteLine("1:相加\n2:相减\n3:相乘\n4:相除\n");
            Console.WriteLine("请从序号1、2、3、4中选择");
            string operateFormat = Console.ReadLine();
            switch (operateFormat)
            {
                case "1":
                    {
                        result = firstNumber + secondNumber;
                        Console.WriteLine(firstNumber + "+" + secondNumber + "=" + result);
                        break;
                    }
                case "2":
                    {
                        result = firstNumber - secondNumber;
                        Console.WriteLine(firstNumber + "-" + secondNumber + "=" + result);
                        break;
                    }
                case "3":
                    {
                        result = firstNumber * secondNumber;
                        Console.WriteLine(firstNumber + "*" + secondNumber + "=" + result);
                        break;
                    }
                case "4":
                    {
                        result = firstNumber / secondNumber;
                        Console.WriteLine(firstNumber + "/" + secondNumber + "=" + result);
                        break;
                    }
            }
            Console.WriteLine("是否继续进行计算(Y/N)?");
            string repeat = Console.ReadLine();
            if ((repeat == "y") || (repeat == "Y"))
                goto input;
            Console.WriteLine("谢谢使用,下次再见!by SDNU咸鱼小十七酱ovo");
        }
    }
}

**

Harvest and experience

The user enters a start to forget to consider unusual circumstances harm, then use try catch capture user input error. In fact, if else was going to want to use to judge the user input error, and then find a good trouble to give up, with the teacher's try catch, CSharp's try catch with nice, really sweet.

Released six original articles · won praise 12 · views 304

Guess you like

Origin blog.csdn.net/zhaoyananzyn/article/details/104600171