【问题标题】:Output not generating results in C#输出未在 C# 中生成结果
【发布时间】: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


【解决方案1】:

看起来你正在解析错误的方式或错误的字段,我建议你应该这样做,在你的文本框控件上附加按键事件并强制用户只输入数字

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
 {
   string text = textBox1.Text;
   return !Regex.IsMatch(text, @"^\d+\.?\d*$";
 }

 private void calculateButton_Click(object sender, EventArgs e)
 {
     decimal grossPay = decimal.Parse(grorssTextBox.text);
     decimal bonus = decimal.Parse(bonusTextBox.text);
     //rest of code 
 }

您也可以将正则表达式代码放入验证函数中,而不是键解析,并验证完整的文本框输入。

【讨论】:

  • 谢谢。我一定会看看这个版本。你知道有什么推荐的 C# 高级学习书籍吗@pranay rana
  • @ABerrio - 我喜欢 jhon skeet 书 C# 的深度和 C# 简而言之......我最喜欢的两本书..那是纯粹的 C# 书,与 UI 无关
【解决方案2】:

您正在尝试解析bonusLabel.Text。我猜bonusLabel 是您的文本框的标签,而不是文本框本身。 TextBox 是不是叫bonusTextBox

...
if (decimal.TryParse(bonusTextBox.Text, out bonus))
...

【讨论】:

  • 谢谢。我很欣赏这个。绝对能解决问题!我对标签和文本框感到困惑。你对任何用于高级学习的 C# 书籍有什么建议吗? @pcdev
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多