我正在使用C#设计一个基本的计算器。当我输入一个小数点时,我遇到了困难。例如,如果我输入.8,那么它会给我0.8,如果显示屏上没有任何东西,那么它是正确的,但在那之后,如果我输入了十进制符号,那么它也会接受0.8……我希望一个数字只接受一个小数符号。我的代码是
private void btn_Decimal_Click(object sender, EventArgs e)
{
if (txt_Result.Text == "" || LastcharIssymbol==true)
{
txt_Result.Text = txt_Result.Text + 0 + ".";
}
else
txt_Result.Text = txt_Result.Text + ".";
}在这里,如果我输入0.9.999,那么它也接受,如果我输入999.....那么它也会接受。我希望一个数字只接受一个小数符号,即999.999。请帮帮我。此外,我还添加了两个额外的标签,可以显示当前系统的日期和时间。我无法显示日期和时间,但我可以使用VB.Net显示日期和时间。我不知道我是从哪里得到错误的。我的整个代码是
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 CS_Calculator
{
public partial class Form1 : Form
{
Boolean LastcharIssymbol {get;set;}
string op;
double a, memory;
public Form1()
{
InitializeComponent();
}
private void btn_1_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "1";
LastcharIssymbol= false;
}
private void btn_2_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "2";
LastcharIssymbol = false;
}
private void btn_3_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "3";
LastcharIssymbol = false;
}
private void btn_4_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "4";
LastcharIssymbol = false;
}
private void btn_5_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "5";
LastcharIssymbol = false;
}
private void btn_6_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "6";
LastcharIssymbol = false;
}
private void btn_7_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "7";
LastcharIssymbol = false;
}
private void btn_8_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "8";
LastcharIssymbol = false;
}
private void btn_9_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "9";
LastcharIssymbol = false;
}
private void btn_0_Click(object sender, EventArgs e)
{
txt_Result.Text = txt_Result.Text + "0";
LastcharIssymbol = false;
}
private void btn_Decimal_Click(object sender, EventArgs e)
{
if (txt_Result.Text == "" || LastcharIssymbol==true)
{
txt_Result.Text = txt_Result.Text + 0 + ".";
}
else
txt_Result.Text = txt_Result.Text + ".";
}
private void btn_Plus_Click(object sender, EventArgs e)
{
if(txt_Result.Text=="" || LastcharIssymbol)
{
MessageBox.Show("Please Enter first number to perform the addition operation.");
}
else
{
op = "+";
txt_Result.Text = txt_Result.Text + op;
LastcharIssymbol=true;
}
}
private void btn_Minus_Click(object sender, EventArgs e)
{
if (txt_Result.Text == "" || LastcharIssymbol)
{
MessageBox.Show("Please enter first number to erform the substraction operation.");
}
else
{
op = "-";
txt_Result.Text = txt_Result.Text + op;
LastcharIssymbol = true;
}
}
private void btn_Division_Click(object sender, EventArgs e)
{
if (txt_Result.Text == "" || LastcharIssymbol)
{
MessageBox.Show("Please enter first number to perform the division operation.");
}
else
{
op = "/";
txt_Result.Text = txt_Result.Text + op;
LastcharIssymbol = true;
}
}
private void btn_Mult_Click(object sender, EventArgs e)
{
if (txt_Result.Text == "" || LastcharIssymbol)
{
MessageBox.Show("Please enter first number to perform the multiplication operation.");
}
else
{
op = "*";
txt_Result.Text = txt_Result.Text + op;
LastcharIssymbol = true;
}
}
private void btn_Equal_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
txt_Result.Text = "";
}
private void btn_Clear_All_Click(object sender, EventArgs e)
{
txt_Result.Text = "";
op = "";
a = 0;
memory = 0;
}
private void btn_Memory_Click(object sender, EventArgs e)
{
memory = Convert.ToDouble(txt_Result.Text);
}
private void btn_Show_Memory_Click(object sender, EventArgs e)
{
txt_Result.Text = memory.ToString();
}
}
}发布于 2013-01-14 13:51:25
if (!txt_Result.Text.Contains("."))
if(txt_Result.Text == string.Empty)
txt_Result.Text = "0.";
else
txt_Result.Text += ".";
else
MessageBox.Show("more dots are not allowd");对于像'11.9+12‘这样的包含操作的文本,您可以执行以下操作
string formula = "11.9/1.2*99.9+19";
string lastPiece = formula.Split(new char[] { '+', '-', '*', '/' })[formula.Split(new char[] { '+', '-', '*', '/' }).Count() - 1];
if (!lastPiece.Contains('.')) formula += ".";
//adds dot
lastPiece = formula.Split(new char[] { '+', '-', '*', '/' })[formula.Split(new char[] { '+', '-', '*', '/' }).Count() - 1];
if (!lastPiece.Contains('.')) formula += ".";
//does not add dot
MessageBox.Show(formula);
//output : 11.9/1.2*99.9+19.发布于 2013-01-14 13:51:56
单击小数后,应禁用小数,如果按下任何运算符或'C‘,则应再次启用小数。
发布于 2013-01-14 13:51:17
您可以使用decimal.TryParse来测试字符串是否为有效的十进制数。如果没有,比如您的输入有两个小数点,那么TryParse调用就会失败。
TryParse是一个很好的选择,因为除了两个小数点之外,输入的数字还可能有许多问题,例如...三个小数点、放错位置的减号、字母字符等。
尝试:
private void btn_Decimal_Click(object sender, EventArgs e)
{
decimal num;
if (!Decimal.TryParse(txt_Result.Text, out num))
{
MessageBox.Show(txt_Result.Text + " is not a valid number.");
return;
}
if (txt_Result.Text == "" || LastcharIssymbol==true)
txt_Result.Text = txt_Result.Text + 0 + ".";
else
txt_Result.Text = txt_Result.Text + ".";
}https://stackoverflow.com/questions/14313810
复制相似问题