当前位置:编程学习 > C#/ASP.NET >>

C#编程,四则运算计算器,我编的个第一位可以输入0的,怎么让第一位输0时为空,还有除数不能为零这些问题

这是我编写的代码,帮帮忙啊

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

namespace 四则运算计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int flag;
        int num1;
        int results;

        private void num1_Click(object sender, EventArgs e)
        {
            Button a = (Button)sender;
            textBox1.Text = textBox1.Text + a.Text;
        }

        private void operator_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (b.Text == "+")
            {
                num1 = int.Parse(textBox1.Text);
                textBox1.Text = "";
                flag = 0;
            }
            if (b.Text == "-")
            {
                num1 = int.Parse(textBox1.Text);
                textBox1.Text = "";
                flag = 1;
            }
            if (b.Text == "*")
            {
                num1 = int.Parse(textBox1.Text);
                textBox1.Text = "";
                flag = 2;
            }
            if (b.Text == "/")
            {
                num1 = int.Parse(textBox1.Text);
                textBox1.Text = "";
                flag = 3;
            }
            if (b.Text == "C")
            {
                num1 = int.Parse(textBox1.Text);
                textBox1.Text = "";
                textBox1.Focus();
            }
            if (b.Text == "=")
            {
                if (flag == 0)
                {
                    results = num1 + int.Parse(textBox1.Text);
                }
                if (flag == 1)
                {
                    results = num1 - int.Parse(textBox1.Text);
                }
                if (flag == 2)
                {
                    results = num1 * int.Parse(textBox1.Text);
                }
                if (flag == 3)
                {
                    results = num1 / int.Parse(textBox1.Text);
                }
                textBox1.Text = results.ToString();
            }
        }

    }
}

追问:那怎么让第一位不输入0的呢不要这样的,第一位只能1-9,最好把代码给我一下哈

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,