【发布时间】:2018-09-01 17:02:01
【问题描述】:
问题: 一个应用程序,可让您输入员工的总工资和奖金金额并计算退休金的金额。
我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Boolean_Method_Application
{
public partial class Form1 : Form
{
private const decimal CONTRIB_RATE = 0.05m;
public Form1()
{
InitializeComponent();
}
// The InputIsValid method converts the user input and stores
//// it in the arguments (passed by reference). If the conversion
///// is successful, the method returns true. Otherwise it returns
//// false.
private bool InputIsValid(ref decimal pay, ref decimal bonus)
{
bool inputGood = false;
if (decimal.TryParse(grossPayTextBox.Text, out pay))
{
if (decimal.TryParse(bonusLabel.Text, out bonus))
{
inputGood = true;
}
else
{
MessageBox.Show("Bonus amount is invalid.");
}
}
else
{
MessageBox.Show("Gross pay is invalid.");
}
return inputGood;
}
private void calculateButton_Click(object sender, EventArgs e)
{
decimal grossPay = 0m, bonus = 0m, contributions = 0m;
if (InputIsValid(ref grossPay, ref bonus))
{
contributions = (grossPay + bonus) * CONTRIB_RATE;
contributionLabel.Text = contributions.ToString("c");
}
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
此问题: 代码似乎是正确的,但是每次我输入数据并单击计算时,我都会不断收到此消息:“奖金金额无效”。代码是否编写不正确或到底发生了什么。我没有输入正确的数据吗?
【问题讨论】:
-
我看到您已将贡献格式化为显示货币。是否有可能 bonusLabel.Text 也被格式化?
-
@mary 实际上是那个小错误。感谢您的帮助。
标签: c# if-statement boolean user-input