为什么在对3个实变量求和时会得到一个奇怪的结果?

问题描述

这是我的代码

program P1;

var a,b,c,p :real;
var i,o,n: string;

begin

i := 'isoscel';
o := 'echilateral';
n :='scalen';

writeln('Introduceti 3 numerele reale');
readln(a,c);


case (a>0) and (b>0) and (c>0) and (a+b>c) and (a+c>b) and (b+c>a) of
 true:
   begin
     p:=a+b+c;
     writeln('perimetrul este ',p);
if (a=b) and (b<>c) or (b=c) and (c<>a) or (a=c) and (c<>b) then write('triunghiul este ',i);
if (a=b) and (a=c) and (b=c) then write('triunghiul este ',o);
if (a<>b) and (b<>c) and (c<>a) then write('triunghiul este ',n);
   end;
 false:
   writeln('nu este posibil');
end;

readln();
end.

例如,如果我插入5 5 5,我将得到以下答案:

perimetrul este  1.5000000000000000E+001
triunghiul este echilateral

但是如果我将a,b,c,p设置为整数而不是实数,则会得到正常结果:

perimetrul este 15
triunghiul este echilateral

我需要我的变量为实数,您能帮助我获得正常答案吗?

解决方法

1.500E001是1.5x10 ^ 1或15。您需要一个格式化的值。试试

writeln(‘Here is the answer: ’,p:5:1)

以5位字段宽度和1个小数位打印结果。根据需要调整格式。