通过if / else语句计算加班时间

时间:2016-02-24 22:03:13

标签: c# webforms

停车场收费5.00美元停车长达3小时。车库每小时额外收费1.50美元,或每小时超过3小时。任何给定24小时的最高费用为18.00美元。假设一次停车的时间不超过24小时。

我正在创建if语句以遵循这些特定规则。我到目前为止尝试过的if语句没有产生我想要的结果。如果声明会涵盖这些规则怎么办?

这是我的.aspx

<!DOCTYPE html> 
<html>
<head> 
<script type="text/javascript"> 
function multiply() {
    var hoursParked = document.getElementById('hP').value;
    var price = 5.00;
    var totalCost = document.getElementById('total');
    var totalPayment = (hoursParked * price);
    if (hours < 3) {
        sum = price * hours;
        return sum;}
    else {
        sum = ((hours - 3) * 0.5 * price) + (price * hours);
        return sum; }
    totalCost.value = sum; }
</script> 
</head>
<body>
<div align="center">
<form id="myForm" runat="server">
<asp:TextBox ID="TextBox1" runat="server" placeholder="Hours parked?" min="1" Max="24"></asp:TextBox><br /><br /> 
<asp:Button ID="Button1" runat="server" Text="Calculate" OnClick="Button1_Click" Width="69px" Height="26px" /><br /><br />
<asp:TextBox ID="TextBox2" runat="server" placeholder="Total Cost" readonly>    </asp:TextBox>
</form> 
</div>
</body>
</html>

最后,我正在尝试创建我的if / else语句。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Project2
{
public partial class Part2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int hours = int.Parse(TextBox1.Text);
        //double total = hours * 5; hours * 5 would apply until the user is parked for more than 3 hours, from there it would change to an additional 1.50
        //if/else statement should go here defining what happens after 3 hours have passed, and what happens when someone parks for 24 hours.
        TextBox2.Text = total.ToString();
    }
  }
}

3 个答案:

答案 0 :(得分:2)

不要在javascript客户端进行计算。

将用户希望停留的时间发送到服务器。

然后

float MinCharge = 5.0;
if (HoursToPark > 3) {
    MinCharge += (HoursToPark-3) * 1.5;
} 
if (MinCharge > 18.0) {
    MinCharge = 18.0;
}

答案 1 :(得分:1)

这应该有效:

if (hours < 3) 
{
    sum = 5;
}
else
{
    sum = 5 + (hours - 3) * 1.50;
}

if (sum > 18)
{
    sum = 18;
}

答案 2 :(得分:1)

如果有疑问,请强行说明:

double GetTotal(int hours) {
   // A parking garage charges $5.00 to park for up to three hours. 
   if (hours <= 3) return 5.0;

   // The garage charges an additional $ 1.50 per hour for each hour or part thereof in excess of three hours. 
   double total = 5;
   total += (hours - 3) * 1.5;

   // The maximum charge for any given 24 hour period is $18.00. 
   if (total > 18) total = 18;

   // Assume that no car parks for longer than 24 hours at a time.
   if (hours > 24) throw new ArgumentOutOfRangeException("hours");

   return total;
}

如果值得的话,你可以随时清理它 - 在这种情况下,我注意到最低费用是5美元,最高费用是18美元,没有其他费用特殊情况:

double GetTotal(int hours) {
   if (hours > 24) throw new ArgumentOutOfRangeException("hours");

   double total = 5.0 + (hours - 3) * 1.5;
   if (total < 5) return 5;
   if (total > 18) return 18;
   return total;
}

哦,我不会用双倍钱(用十进制代替)。