我正在設計一個使用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();
}
}
}
請一次提出一個問題。你的標籤和日期/時間碼在哪裏?請修改代碼。大部分是不必要的。請只顯示給你的問題。另外,這功課呢? –
謝謝保羅。我已經完成日曆顯示日期和時間。我寫了 lbl_Time_Disp.Text = DateTime.Now.ToLongTimeString(); lbl_Date_Disp.Text = DateTime.Now。ToLongDateString(); 在Form_Load事件和Timer_Tick事件下我寫了 lbl_Time_Disp.Text = DateTime.Now.ToLongTimeString(); 我認爲這段代碼是正確的。 – Pritam
建議@Pritam,儘量不要讓你的問題變得混亂,否則人們不會讀到最後,因爲它就像是一本2000頁的書:) –