You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ada语言停车场收费代码1-24小时对应if分支被跳过问题求助

问题根源

你的代码中外层判断逻辑存在逻辑错误,导致1.00~24.00区间的输入根本无法进入包含目标分支的代码块:

-- 原错误外层条件:要求同时满足>1.00和>24.00,等价于仅大于24.00的数值才能进入内部代码块
if carTime > 1.00 and carTime > 24.00 then

当输入的停车时长在1.00到24.00之间时,不满足该外层判断,因此整个内部的代码段都不会执行,同时你没有给paye设置初始值,所以函数返回了默认的0.00结果。

修复方案

  1. 修正外层判断条件,直接改为if carTime > 1.00 then即可,让所有大于1小时的情况都能进入内部处理逻辑
  2. 建议给paye变量设置初始值,避免出现未赋值就返回的异常情况
  3. 浮点型直接判断相等存在精度误差风险,建议调整判断顺序覆盖24.00的边界场景

修复后的calculateCharges函数参考:

function calculateCharges(carTime : in Float; paye : out Float) return Float is
   BASE_CHARGE : constant Float := 2.00;
   DAY_CHARGE : constant Float := 10.00;
   EXTRA_CHARGE : constant Float := 0.50;
   timeOverHour : Float := 0.00;
   overPay : Float := 0.00;
begin
   -- 先给paye设置初始值避免未赋值
   paye := BASE_CHARGE;

   if carTime <= 1.00 then
      paye := BASE_CHARGE;
   elsif carTime < 24.00 then
      timeOverHour := carTime - 1.00;
      overPay := timeOverHour * EXTRA_CHARGE;
      paye := BASE_CHARGE + overPay;
   else -- 覆盖等于24和大于24的所有场景
      timeOverHour := carTime - 24.00;
      overPay := timeOverHour * EXTRA_CHARGE;
      paye := DAY_CHARGE + overPay;
   end if;

   return paye;
end calculateCharges;

内容的提问来源于stack exchange,提问作者Ada Lovelace

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 00:36:03