| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2120 人关注过本帖
标题:计算机应用程序
只看楼主 加入收藏
yongyuande01
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2016-3-14
结帖率:0
收藏
已结贴  问题点数:10 回复次数:2 
计算机应用程序
编写一个能够加减乘除的计算机程序,窗体应用程序,并且能够处理可能产生的异常
搜索更多相关主题的帖子: 计算机程序 计算机应用 应用程序 
2016-04-25 16:02
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:10 
这个是很基本的东西吧,异常可以用try,catch来捕捉。

   唯实惟新 至诚致志
2016-04-25 22:47
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormTest
{
    public partial class Form1 : Form
    {
        private double n1;
        private double n2;

        public Form1()
        {
            InitializeComponent();

            this.n1 = 0;
            this.n2 = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;
            this.Text = "Calculator";

            this.comboBox1.Items.Add("+");
            this.comboBox1.Items.Add("-");
            this.comboBox1.Items.Add("x");
            this.comboBox1.Items.Add("/");
            this.comboBox1.SelectedIndex = 0;

            this.label1.Text = "=";

            this.button1.Text = "OK";
            this.button2.Text = "Exit";
            this.button3.Text = "Clear";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!txtValidate()) return;

            double result = 0d;

            if(this.comboBox1.SelectedIndex==0)
            {
                result = this.n1 + this.n2;
            }
            else if(this.comboBox1.SelectedIndex==1)
            {
                result = this.n1 - this.n2;
            }
            else if(this.comboBox1.SelectedIndex==2)
            {
                result = this.n1 * this.n2;
            }
            else
            {
                result = this.n1 / this.n2;
            }

            this.textBox3.Text = result.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox1.Clear();
            this.textBox2.Clear();
            this.textBox3.Clear();
            this.comboBox1.SelectedIndex = 0;
        }

        private bool txtValidate()
        {
            try
            {
                this.n1 = double.Parse(this.textBox1.Text);
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }

            try
            {
                this.n2 = double.Parse(this.textBox2.Text);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }

            if(this.comboBox1.SelectedIndex==3&&double.Parse(this.textBox2.Text)==0d)
            {
                MessageBox.Show("Can't divide zero!");
                return false;
            }

            return true;
        }
    }
}

   唯实惟新 至诚致志
2016-04-26 13:54
快速回复:计算机应用程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019124 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved