[英]How do I tell the my Program that it should keep subtracting from a number?
我有以下代碼:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
double salaryAmount;
double subtractAmount;
double newSalaryAmount;
string amountBackToString;
private void Button1_Click(object sender, RoutedEventArgs e)
{
try
{
salaryAmount = double.Parse(TextBox1.Text);
label1.Content = salaryAmount.ToString();
}
catch (Exception)
{
MessageBox.Show("This was not a valid input.");
}
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
try
{
subtractAmount = double.Parse(TextBox2.Text);
newSalaryAmount = salaryAmount - subtractAmount;
label1.Content = newSalaryAmount.ToString();
}
catch (Exception)
{
MessageBox.Show("This was not a valid input.");
}
}
private void Button3_Click(object sender, RoutedEventArgs e)
{
amountBackToString = Convert.ToString(newSalaryAmount);
string[] currentAmount = { amountBackToString };
File.WriteAllLines(@"C:\Users\Silas\Downloads\Lohn.txt", currentAmount);
}
private void Button4_Click(object sender, RoutedEventArgs e)
{
string text = File.ReadAllText(@"C:\Users\Silas\Downloads\Lohn.txt");
label1.Content = text;
salaryAmount = Convert.ToDouble(label1.Content);
}
}
問題是它每次都會將數字重置為初始輸入,然后減去,但它應該采用初始輸入並不斷從中減去
我的程序是薪水計算器。
您只需要將newSalaryAmount
應用於當前的salaryAmount
private void Button2_Click(object sender, RoutedEventArgs e)
{
try
{
subtractAmount = double.Parse(TextBox2.Text);
newSalaryAmount = salaryAmount - subtractAmount;
//Set the new current salary
salaryAmount = newSalaryAmount
label1.Content = newSalaryAmount.ToString();
}
catch (Exception)
{
MessageBox.Show("This was not a valid input.");
}
}
在您的情況下, salaryAmount
將始終保持從TextBox1.Text
給定的初始值,因此當您再次減少該值時,它將采用此后沒有改變的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.