我正在嘗試在 Windows 表單應用程式中制作一個零錢計算器,我需要在其中顯示客戶將獲得退款的賬單。我的問題是文本框只會顯示我的 if 陳述句中的一個值。
例如
如果客戶需要 644 回來,它只說 1 500 克朗。它應該顯示如下:1 500 鈔票 1 100 鈔票 2 20 硬幣 2 2 硬幣。希望你能幫忙:)
公共無效現金計算器(){
fiveHundreds = exchange / 500;
remainder = exchange % 500;
twoHundreds = remainder / 200;
remainder = remainder % 200;
hundreds = remainder / 100;
remainder = remainder % 100;
fiftys = remainder / 50;
remainder = remainder % 50;
twenties = remainder / 20;
remainder = remainder % 20;
tens = remainder / 10;
remainder = remainder % 10;
fives = remainder / 5;
remainder = remainder % 5;
twos = remainder / 2;
remainder = remainder % 2;
ones = remainder;
// if statements to sort out what cashtype that will be shown in lblCashType:
if (ones >0)
{
tbxCashType.Text = ones " Enkronor";
}
if (twos > 0)
{
tbxCashType.Text = twos " Tv?kronor";
}
if (fives > 0)
{
tbxCashType.Text = fives " Femkronor";
}
if (tens > 0)
{
tbxCashType.Text = tens " Tikronor";
}
if (twenties > 0)
{
tbxCashType.Text = twenties " Tjugolapp";
}
if (fiftys > 0)
{
tbxCashType.Text = fiftys " Femtiolapp";
}
if (hundreds > 0)
{
tbxCashType.Text = hundreds " Hundralapp";
}
if (twoHundreds > 0)
{
tbxCashType.Text = twoHundreds " Tv?hundralapp";
}
if (fiveHundreds > 0)
{
tbxCashType.Text = fiveHundreds " Femhundralapp";
}
uj5u.com熱心網友回復:
您的錯誤是您用屬性的新文本覆寫了每個 if 陳述句中的舊文本Text。例如,如果要將“twos”添加到“ones”,則需要撰寫
tbxCashType.Text = twos " Tv?kronor";
=運算子將右側的字串添加到現有字串值。所以你可以組合多個字串(concat strings)。
如果你想添加一個完整的新字串,那么你只需要=. 這會覆寫舊值。
請參閱 C# 檔案:https ://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/addition-operator#addition-assignment-operator-
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/433990.html
上一篇:使用依賴注入初始化主表單中的介面
