爲作業做一個更改制作程序,它必須在輸入金額時返回更改金額(它基於澳大利亞貨幣),並且我已將其工作到50美分。當計算與變化,程序必須返回一個二十美分,十美分的值或五分變化,程序凍結C#更改制作程序
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
double change = Convert.ToDouble(txtOffered.Text) - Convert.ToDouble(txtDue.Text);
// MessageBox.Show(change.ToString());
double hund = 100;
double fifty = 50;
double twent = 20;
double ten = 10;
double five = 5;
double two = 2;
double one = 1;
double fifcent = 0.50;
double twentcent = 0.20;
double tencent = 0.10;
double fivecent = 0.05;
while (change > 0)
{
if (change >= hund)
{
txtChange.Text += "1x $100 \r\n";
change = change - hund;
}
else if (change >= fifty)
{
txtChange.Text += "1x $50 \r\n";
change = change - fifty;
}
if (change >= twent)
{
txtChange.Text += "1x $20 \r\n";
change = change - twent;
}
else if (change >= ten)
{
txtChange.Text += "1x $10 \r\n";
change = change - ten;
}
if (change >= five)
{
txtChange.Text += "1x $5 \r\n";
change = change - five;
}
else if (change >= two)
{
txtChange.Text += "1x $2 \r\n";
change = change - two;
}
if (change >= one)
{
txtChange.Text += "1x $1 \r\n";
change = change - one;
}
else if (change >= fifcent)
{
txtChange.Text += "1x 50c \r\n";
change = change - fifcent;
}
if (change >= twentcent)
{
txtChange.Text += "1x 20c \r\n";
change = change - twentcent;
}
else if (change >= tencent)
{
txtChange.Text += "1x 10c \r\n";
change = change - tencent;
}
if (change >= fivecent)
{
txtChange.Text += "1x 5c \r\n";
change = change - fivecent;
}
}
}
}
那麼這裏是你的時間來學習如何調試,跟蹤應用程序,看看它卡住的地方! – BugFinder
這應該不會導致應用程序凍結,但是您應該可以在任何地方使用「else if」,除了第一個「if」 – dlxeon