繁体   English   中英

如何检查文本框中的数字是否大于预定义的值

[英]How do I check if a number in a textbox is greater than a predefined value

我正在做一个运动,我必须申请一个停车场。 目的是:创建一个带有numericUpDown控件的表单,用于选择停放车辆的时间。 添加选项按钮以显示车辆是轿车还是卡车。 清楚地标记每个框和按钮。 添加标签或文本框以显示要支付的金额。

另外,我必须设置一些限制。 汽车24小时的付款不应超过$ 38.00,卡车24小时的付款不应超过$ 44.50。

我发现的唯一问题是我无法设置这些限制。 它给出一个错误:使用未分配的局部变量'result'。

检查注释的///代码。

任何帮助,不胜感激。 这不是一个功课项目,我只是自己学习c#并做一些练习。

这是代码:

        private void calculate_Click(object sender, EventArgs e)
    {
        double carhours = 3;
        double truckhours = 3.50;
        double carFirsthour = 5;
        double truckFirsthour = 6;
        int hrs = (int)numericUpDown.Value;
        double result;
        int maxCarFee = 38;


        if (numericUpDown.Value < 1)
        {
            MessageBox.Show("NO WAY");
            return;
        }

        if (carButton.Checked == true && (numericUpDown.Value == 1))
        {
            result = (hrs * carFirsthour);
            fee.Text = "$" +  result;
        }

        if (carButton.Checked == true && (numericUpDown.Value > 1))
        {
            result = ((hrs - 1) * carhours + carFirsthour);
            fee.Text = "$" + result;
        }

        if (truckButton.Checked == true && (numericUpDown.Value == 1))
        {
            result = (hrs * truckFirsthour);
            fee.Text = "$" + result;
        }

        if (truckButton.Checked == true && (numericUpDown.Value > 1))
        {
            result = ((hrs - 1) * truckhours + truckFirsthour);
            fee.Text = "$" + result;
        }

        /// limits

        ///if (carButton.Checked == true && (numericUpDown.Value < 24) && (result > maxCarFee))
        ///{
        ///    fee.Text = "$" + maxCarFee;
        ///}
    }

您需要将numericUpDown.Value转换为int32

或因为您这样做:

int hrs = (int)numericUpDown.Value; 

您可以在if语句中使用hrs而不是numericUpDown.Value

尝试这样的事情:

private void calculate_Click(object sender, EventArgs e) {
    double carhours = 3.0;
    double truckhours = 3.5;
    double carFirsthour = 5.0;
    double truckFirsthour = 6.0;
    double maxCarFee = 38.0;
    double maxTruckFee = 44.5;

    int hrs = (int)numericUpDown.Value;
    double result;

    if (hrs < 1) {
        MessageBox.Show("NO WAY");
        return;
    }

    if (carButton.Checked) {
        result = (hrs-1) * carhours + carFirsthour;
        if (result > maxCarFee) {
            result = maxCarFee;
        }
    }
    else {
        result = (hrs-1) * truckhours + truckFirsthour;
        if (result > maxTruckFee) {
            result = maxTruckFee;
        }
    }

    fee.Text = "$" +  result;
}

但是,您实际上并不清楚如果汽车/卡车停留超过24小时(或超过48小时)会发生什么情况。 您可能会得到更一致的结果,例如:

private void calculate_Click(object sender, EventArgs e) {
    double hourlyFee = 3.0;
    double firstHourFee = 5.0;
    double max24hrFee = 38.0;

    if (!carButton.Checked) {
       hourlyFee = 3.5;
       firstHourFee = 6.0;
       max24hrFee = 44.5;
    }

    int hrs = (int)numericUpDown.Value;
    if (hrs < 1) {
        MessageBox.Show("NO WAY");
        return;
    }

    double result = 0.0;
    if (hrs >= 24) {
       result = (hrs / 24) * max24hrFee;
       hrs = hrs % 24;
    }

    if (hrs > 0) {
       result = result + firstHourFee + ((hrs-1) * hourlyFee);
    }

    fee.Text = "$" +  result;
}

您检查是否numericUpDown.Value小于24,我想您是说大于(>)?!

使用未分配的局部变量“结果”

表示您不初始化变量。 你必须做这样的事情

result = 0 

在转到if构造之前。

问题是没有条件(if)语句被执行,因此result永远不会被赋值。 在声明中分配result - double result = 0D; -代码将运行,因此您可以调试条件逻辑。

int hrs = (int)numericUpDown.Value;

您会获得以小时为单位的价值,因此可以在if条件下使用它。

if (carButton.Checked == true && (numericUpDown.Value == 1))

像下面这样写。 无需== true

if (carButton.Checked && (numericUpDown.Value == 1))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM