我有一个问题问你。我需要在每一行中写入最大元素。例如,我的桌子:
1 2 3 4
5 6 7 8
9 10 11 12
我想得到4,8,12我尝试过但没有结果:
Program Lab2;
type A=array[1..5,1..5] of integer;
var x:A;
i,j,s,max:integer;
Begin
writeln('Write date:');
for i:=1 to 5 do
for j:=1 to 5 do
read(x[i,j]);
for i:=1 to 5 do
for j:=1 to 5 do
begin
max:=x[i,1];
if (max<x[i,j]) then max:=x[i,j];
writeln(max);
end;
readln;
请帮我结束。
只有三个小错误:
1)if (max<x[i,j])
应该在第二个for循环之外,因为您只想每行一次初始化最大值。
2)writeln(max);
应该在第二个for循环之外,您只想每行打印一次该值。
3) read(x[i,j]);
我建议这样做是readln (x[i,j])
因为读时您只能读一个字符,读时您可以读红色字符,直到找到新的换行符为止,这样您就可以输入两位以上的数字了。
这仅对字符串有意义,可以使用read
或readln
与整数一起使用
我还建议您在编写begin
contol结构(for,while,if等)的同一行中编写关键字,因为这样更类似于C编码风格约定,这是最流行的编码风格之一我猜。如果您尝试对任何一种语言都保持类似的编码风格,那么这对您也更好。
所以代码将是:
Program Lab2;
const SIZE=3;
type A=array [1..SIZE,1..SIZE] of integer;
var x:A;
i,j,max:integer;
Begin
writeln('Write date:');
for i:=1 to SIZE do begin
for j:=1 to SIZE do begin
readln(x[i,j]);
end;
end;
for i:=1 to SIZE do begin
max:=x[i,1];
for j:=1 to SIZE do begin
if (max<x[i,j]) then begin
max:=x[i,j];
end;
end;
writeln('the max value of the row ',i ,' is ',max);
end;
readln;
readln;
end.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句