Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在计算器中哪里使用异常处理?_C#_Exception Handling - Fatal编程技术网

C# 在计算器中哪里使用异常处理?

C# 在计算器中哪里使用异常处理?,c#,exception-handling,C#,Exception Handling,我已经创建了计算器。它最终运行,但一旦我开始输入任何不正确的内容,它就会抛出一个FormatException。我在下面附上我的代码,请建议我如何处理我的程序中的异常 MyCalculator.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.

我已经创建了计算器。它最终运行,但一旦我开始输入任何不正确的内容,它就会抛出一个
FormatException
。我在下面附上我的代码,请建议我如何处理我的程序中的异常

MyCalculator.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyCalculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        float number, answer;
        int count;

        public void disable() //create new method to disable calculator
        {
            //follow are disable when call we disable() function
            Display.Enabled = false;
            On.Show();//it will be still display
            offbutton.Hide(); //it will be hide
            back.Enabled = false;
            clear.Enabled = false;
            Add.Enabled = false;
            Sub.Enabled = false;
            Multiply.Enabled = false;
            Divide.Enabled = false;
            Zero.Enabled = false;
            One.Enabled = false;
            Two.Enabled = false;
            Three.Enabled = false;
            Four.Enabled = false;
            Five.Enabled = false;
            Six.Enabled = false;
            Seven.Enabled = false;
            Eight.Enabled = false;
            Nine.Enabled = false;
            equal.Enabled = false;
            Point.Enabled = false;
        }

        public void enable() //create new method to enable calculator
        {
            //follow are enable we call enable() function
            Display.Enabled = true;
            On.Hide();//it will be hide
            offbutton.Show();//it will be still display 
            back.Enabled = true;
            clear.Enabled = true;
            Add.Enabled = true;
            Sub.Enabled = true;
            Multiply.Enabled = true;
            Divide.Enabled = true;
            Zero.Enabled = true;
            One.Enabled = true;
            Two.Enabled = true;
            Three.Enabled = true;
            Four.Enabled = true;
            Five.Enabled = true;
            Six.Enabled = true;
            Seven.Enabled = true;
            Eight.Enabled = true;
            Nine.Enabled = true;
            equal.Enabled = true;
            Point.Enabled = true;
        }
        private void Point_Click(object sender, EventArgs e)
        {
         //Display dot(.) in textbox when press dot(.) button with red color
            Display.Text = Display.Text+".";
            Display.ForeColor = Color.Red;
        }

        private void Zero_Click(object sender, EventArgs e)
        {
       //Display zero(0) in textbox when press zero(0) button with red color
            Display.Text = Display.Text + 0;
            Display.ForeColor = Color.Red;
        }

        private void One_Click(object sender, EventArgs e)
        {
            //Display 1 in textboc when press 1 button with red color
            Display.Text = Display.Text + 1;
            Display.ForeColor = Color.Red;
        }

        private void Two_Click(object sender, EventArgs e)
        {
            //Display 2 in textboc when press 2 button with red color
            Display.Text = Display.Text + 2;
            Display.ForeColor = Color.Red;
        }

        private void Three_Click(object sender, EventArgs e)
        {
            //Display 3 in textboc when press 3 button with red color
            Display.Text = Display.Text + 3;
            Display.ForeColor = Color.Red;
        }

        private void Four_Click(object sender, EventArgs e)
        {
            //Display 4 in textboc when press 4 button with red color
            Display.Text = Display.Text + 4;
            Display.ForeColor = Color.Red;
        }

        private void Five_Click(object sender, EventArgs e)
        {
            //Display 5 in textboc when press 5 button with red color
            Display.Text = Display.Text + 5;
            Display.ForeColor = Color.Red;
        }

        private void Six_Click(object sender, EventArgs e)
        {
            //Display 6 in textboc when press 6 button with red color
            Display.Text = Display.Text + 6;
            Display.ForeColor = Color.Red;
        }

        private void Seven_Click(object sender, EventArgs e)
        {
            //Display 7 in textboc when press 7 button with red color
            Display.Text = Display.Text + 7;
            Display.ForeColor = Color.Red;
        }

        private void Eight_Click(object sender, EventArgs e)
        {
            //Display 8 in textboc when press 8 button with red color
            Display.Text = Display.Text + 8;
            Display.ForeColor = Color.Red;
        }

        private void Nine_Click(object sender, EventArgs e)
        {
            //Display 9 in textboc when press 9 button with red color
            Display.Text = Display.Text + 9;
            Display.ForeColor = Color.Red;
        }

        private void offbutton_Click(object sender, EventArgs e)//off button
        {
            disable(); //call disable to off calculator
        }

        private void On_Click(object sender, EventArgs e) //on button
        {
            enable(); //call enable function to on calculator
        }

        public void compute()
        {

            switch (count) //creating switch statement
            {
                 case 1:
                 answer = number + float.Parse(Display.Text);
                 //it performs addition

                 Display.Text = answer.ToString();              
                 //converts float into string
                 break;
                 case 2:
                     answer = number - float.Parse(Display.Text); 
//it performs subtraction
                     Display.Text = answer.ToString();              //converts float into string
                     break;
                 case 3:
                     answer = number * float.Parse(Display.Text);   //it performs Multiplication
                     Display.Text = answer.ToString();              //converts float into string
                     break;
                  case 4:
                      answer = number / float.Parse(Display.Text);   //it performs Division
                      Display.Text = answer.ToString();              //converts float into string
                      break;
                   default:
                      break;
                }

        }

        private void Add_Click(object sender, EventArgs e)
        {
            number = float.Parse(Display.Text);
            Display.Clear(); //clear the textbox
            Display.Focus(); //focus on textbox after clear
            count = 1; //count store case
            label1.Text = number.ToString() + "+"; //display text on lable
        }

        private void Sub_Click(object sender, EventArgs e)
        {
            number = float.Parse(Display.Text);
            Display.Clear(); //clear the textbox
            Display.Focus(); //focus on textbox after clear
            count = 2; //count store switch case value
            label1.Text = number.ToString() + "-"; //display text on lable

        }

        private void Multiply_Click(object sender, EventArgs e)
        {
            number = float.Parse(Display.Text);
            Display.Clear(); //clear the textbox
            Display.Focus(); //focus on textbox after clear
            count = 3; //count store switch case value
            label1.Text = number.ToString() + "*"; //display text on lable
        }

        private void Divide_Click(object sender, EventArgs e)
        {
            number = float.Parse(Display.Text);
            Display.Clear(); //clear the textbox
            Display.Focus(); //focus on textbox after clear
            count = 4; //count store switch case value
            label1.Text = number.ToString() + "/"; //display text on lable
        }

        private void equal_Click(object sender, EventArgs e)
        {
            compute();//call compute function to perform such operations
            label1.Text = "";//clear the text on the lable
        }

        private void clear_Click(object sender, EventArgs e)//clear button
        {
            Display.Text = ""; //clear the textbox
        }

        private void back_Click(object sender, EventArgs e)//backspace button
        {
            int length = Display.TextLength - 1;
            string text = Display.Text;
            Display.Clear();
            for (int i = 0; i < length; i++)
                Display.Text = Display.Text + text[i];

        }


    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间MyCalculator
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
浮点数,答案;
整数计数;
public void disable()//创建新方法以禁用计算器
{
//调用disable()函数时,下面是disable
Display.Enabled=false;
On.Show();//它仍将显示
offbutton.Hide();//它将被隐藏
back.Enabled=false;
clear.Enabled=false;
Add.Enabled=false;
Sub.Enabled=false;
Multiply.Enabled=false;
Divide.Enabled=false;
零。启用=错误;
1.启用=错误;
2.启用=错误;
三、启用=假;
四、启用=假;
5.启用=错误;
6.启用=错误;
7.启用=错误;
8.启用=错误;
9.启用=错误;
equal.Enabled=false;
Point.Enabled=false;
}
public void enable()//创建新方法以启用计算器
{
//下面是我们称之为enable()函数的enable
Display.Enabled=true;
On.Hide();//它将被隐藏
offbutton.Show();//它仍将显示
back.Enabled=true;
clear.Enabled=true;
Add.Enabled=true;
Sub.Enabled=true;
Multiply.Enabled=true;
Divide.Enabled=true;
零。启用=真;
一、启用=真;
2.启用=真;
三、启用=真;
四、启用=真;
五、启用=真;
六、启用=真;
七、启用=真;
八、启用=真;
九、启用=真;
equal.Enabled=true;
Point.Enabled=true;
}
私有无效点击(对象发送者,事件参数e)
{
//按红色的点(.)按钮时,在文本框中显示点(.)
Display.Text=Display.Text+“;
Display.ForeColor=Color.Red;
}
私有无效零点击(对象发送者,事件参数e)
{
//按红色的零(0)按钮时,在文本框中显示零(0)
Display.Text=Display.Text+0;
Display.ForeColor=Color.Red;
}
私有无效单击一次(对象发送者,事件参数e)
{
//按红色按钮1时,在文本框中显示1
Display.Text=Display.Text+1;
Display.ForeColor=Color.Red;
}
私有无效两次单击(对象发送者,事件参数e)
{
//按红色的2按钮时,在文本框中显示2
Display.Text=Display.Text+2;
Display.ForeColor=Color.Red;
}
私有无效三次单击(对象发送者,事件参数e)
{
//按红色的3按钮时,在文本框中显示3
Display.Text=Display.Text+3;
Display.ForeColor=Color.Red;
}
私有无效四次单击(对象发送者,事件参数e)
{
//按红色的4按钮时,在文本框中显示4
Display.Text=Display.Text+4;
Display.ForeColor=Color.Red;
}
私有无效五次单击(对象发送者,事件参数e)
{
//按红色的5按钮时,在文本框中显示5
Display.Text=Display.Text+5;
Display.ForeColor=Color.Red;
}
私有无效六次单击(对象发送者,事件参数e)
{
//按红色的6按钮时,在文本框中显示6
Display.Text=Display.Text+6;
Display.ForeColor=Color.Red;
}
私有void Seven_Click(对象发送方,事件参数e)
{
//按红色的7按钮时,在文本框中显示7
Display.Text=Display.Text+7;
Display.ForeColor=Color.Red;
}
私有无效八次单击(对象发送者,事件参数e)
{
//按红色的8按钮时,在文本框中显示8
Display.Text=Display.Text+8;
Display.ForeColor=Color.Red;
}
私有void Nine_Click(对象发送方,事件参数e)
{
//按红色的9按钮时,在文本框中显示9
Display.Text=Display.Text+9;
Display.ForeColor=Color.Red;
}
私有无效关闭按钮\单击(对象发送者,事件参数)//关闭按钮
{
disable();//调用disable关闭计算器
}
单击(对象发送者,事件参数)//On按钮时出现私有无效
{
enable();//在计算器上调用enable函数
}
公共空间计算()
{
switch(count)//创建switch语句
{
案例1:
answer=number+float.Parse(Display.Text);
//它执行加法运算
Display.Text=answer.ToString();
//将浮点转换为字符串
打破
案例2:
answer=number-float.Parse(Display.Text);
//它执行减法运算
Display.Text=answer.ToString();
if(!float.TryParse(Display.Text, out number))
    return;

//rest of your code
try
{
   //Do your logic here !
}
catch(Exception ex) //Here you can catch any type of Exception 
{                   //like InvalidFormatException, and even print out ex.Message
   //Do something
   //Logging, MessageBox(Invalid operation ..), ...
}