在计算器显示中添加点 [英] adding a dot in a calculator display

查看:107
本文介绍了在计算器显示中添加点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!
我正在做C#教程(homeandlearn.co.uk),目前正在制作计算器.
但是,我已经到了让计算器接受点"的地步,我不知道用什么符号"来表示点...好吧,这是代码:


Hello!
I''m doing a C# tutorial (homeandlearn.co.uk) and i''m currently making a calculator.
However, I''ve gotten to the point where I''ll make the calculator accept "dots" and I dont know what "symbol" to use for just dot... Alright, here''s the code:


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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double total1 = 0;
        double total2 = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button1.Text;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button2.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button3.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button9.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button6.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button5.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button4.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button8.Text;
        }

        private void button0_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + button0.Text;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            total1 = total1 + double.Parse(textBox1.Text);
            textBox1.Clear();
        }

        private void btnEquals_Click(object sender, EventArgs e)
        {
            total2 = total1 + double.Parse(textBox1.Text);
            textBox1.Text = total2.ToString();
            total1 = 0;
        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + ".";
        }
    }
}



请注意,btn点在末尾.

在如此完善的地方再次对那些令人讨厌的问题再次感到抱歉.
[b]在当前状态下,每当我按+或=,[/b]
时,程序就会崩溃

本课说:您尚未为btnPoint按钮编写任何代码.这意味着添加的内容中不能包含10.5或36.7之类的数字.请编写代码来解决此问题.(提示:您只需要一行代码)."





我得到的错误:



Please do note that the btn point is at the end.

Again sorry for the noobish questions at a such well established place as this.
[b]At its current state, the program crashes whenever i press +, or =, [/b]


The lesson said this: "You have not yet written any code for the btnPoint button. This means that you can''t have numbers like 10.5 or 36.7 in your additions. Write code to solve this. (Hint: you only need one line of code.)"





error i get:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.FormatException: Input string was not in a correct format.
   at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
   at WindowsFormsApplication1.Form1.btnPlus_Click(Object sender, EventArgs e) in C:\Users\Weidrup\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs:line 82
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.225 (RTMGDR.030319-2200)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
WindowsFormsApplication1
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/Weidrup/AppData/Local/Temporary%20Projects/WindowsFormsApplication1/bin/Release/WindowsFormsApplication1.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

推荐答案

这是一种常见的初学者技巧为每个按钮单击事件创建一个按钮处理程序;但是绝对没有必要.

It is a common beginner technique to create a button handler for each button click event; however it is absolutely unnecessary.

protected void OnButtonClick(object sender, EventArgs e)
{
  textBox1.Text += ((Button)sender).Tag;
}



为按钮的Tag属性分配适当的数字或符号(小数点的句号是正确的),您将获得更整洁,更小的代码.



Assign the proper number or symbol (period is correct for decimal point) to the Tag property for the button and you will have much cleaner and smaller code.


您可能已配置系统使用,"作为小数点分隔符而不是.".

尝试使用逗号,看看它是否有效.
You''re system is probably configured to use a "," as a decimal separator and not a ".".

Try using a comma and see if it works.


嗯,您应该使用十进制值验证每个用户输入.这可以通过.NET正则表达式RegEx类来实现.因此,对于每个数字按钮,在执行加法运算之前,都要使输入值生效.
这是验证值的方法.
Well, You should validate each user input with decimal value. This can be achieved with .NET Regular Expression, RegEx class.So for each number button validdate the input value, before performing the addition operation.

Here is how you will validate the value.
if (!Regex.IsMatch(textBox1.Text, "^(-)?\\d*(\\.\\d+)?


这篇关于在计算器显示中添加点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆