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

Calculation of overtime though if/else statement

停车场最多收费 5.00 美元,最多可停三个小时。车库对超过三小时的每小时或不足一小时额外收费 1.50 美元。任何给定 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();
    }
  }
}

这应该有效:

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

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

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

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

然后

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

如有疑问,暴力破解:

double GetTotal(int hours) {
   // A parking garage charges .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 .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;
}

哦,我不会用双倍的钱(用小数代替)。