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

Pascal统计日气温程序最低温数值错误但对应小时正确求助

问题原因

  • 最核心的错误是min_t := t[0];的初始化位置写在了for循环内部,每轮输入新小时的温度后,都会把当前最小值重置为0点的温度,导致之前遍历得到的最小值被覆盖,只有本轮输入的温度比0点温度低时才会临时更新最小值,下一轮循环立刻又被重置,所以最终最低温度结果错误。
  • 变量名不统一:你全局声明的最低/最高温对应小时变量是hour_t_min、hour_t_max,但赋值时写的是ora_t_min、ora_t_max,属于拼写错误,会导致未定义变量报错,你提到小时输出正确应该是本地代码拼写一致,贴代码时出现了笔误。
  • 初始值缺失:max_t、hour_t_min、hour_t_max都没有设置初始值,极端情况下如果所有温度都低于初始随机值/高于初始随机值,会导致结果错误。

修正后的代码

Program P1;

Type 
    Hour = 0..23;
    Degrees = -40..40;
    Temperature = array [Hour] of Degrees;

var 
    t : Temperature;
    i, min_t, max_t, hour_t_min, hour_t_max : integer;

    procedure test;
    begin
        // 初始值放在循环外,用0点的温度作为初始最值
        min_t := t[0];
        max_t := t[0];
        hour_t_min := 0;
        hour_t_max := 0;
        for i := 0 to 23 do
        begin
            writeln('Enter the temperature at hour ', i);
            readln(t[i]);
         
            if min_t > t[i] then 
            begin 
                min_t := t[i]; 
                hour_t_min := i; 
            end;

            if max_t < t[i] then 
            begin 
                max_t := t[i]; 
                hour_t_max := i; 
            end;
        end;
    
        writeln('temp min ', min_t, ' at hour ', hour_t_min);
        writeln('temp max ', max_t, ' at hour ', hour_t_max);
    end; {procedure}

begin { main }
    test;

end.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 17:48:02