0
I次のコードを持っている:私のプログラムは数値から引き算を続けなければならないと教えてください。
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);
}
}
問題は、それが数に初期入力するたびにリセットしてから減算しますが、それは初期入力を取り、それが
私のプログラムから引いておくべきであるということです給料計算機です。あなたは単にあなたが再び値を小さくするために行くだろうというときsalaryAmount
はいつもTextBox1.Text
から与えられた初期値とどまるあなたのケースでは、あなたの現在の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.");
}
}
にnewSalaryAmount
を適用する必要が
ありがとうございました! – Paludis
@パルディス歓迎です:) – CNuts