代码没有移动过去阅读
Code not moving past Read
我的代码有问题。它开始很好,但是当开始要求从用户那里获取输入时,它不会移动到 if 语句。我怎样才能解决这个问题。我试过堆来解决这个问题并最终让它读取数据但不断说它无效。
PROGRAM AT3 (input, output);
uses crt, math;
CONST
band6 = 90;
band5 = 80;
band4 = 70;
band3 = 60;
band2 = 50;
VAR
Studname : array of string;
studmark : array of integer;
flag : boolean;
studinfo : text;
input : string;
count : integer;
num : integer;
input2: integer;
highmark, lowmark : integer;
median, average : integer;
BEGIN
lowmark := 100;
highmark := 0;
median := 0;
ASSIGN (Studinfo, 'ExamResults.txt');
flag := false;
WRITELN('welcome to the Band generator.');
WRITELN('To enter student results, please enter the number of students. To see class statistics, please type zzz. To clear the screen type clear screen. And to exit the program, type exit');
While flag = false DO
ReadLN (input);
IF input = 'zzz' THEN
WHILE not EOF(Studinfo) DO
BEGIN
WRITELN(studinfo);
END;
IF input = 'exit' THEN
Flag := true;
IF input = 'clear screen' THEN
CLRSCR
ELSE
if input2 <> 0 THEN
num := input2
ELSE
WRITELN('Please enter a valid number.');
FOR count := 0 to num-1 DO
BEGIN
WRITELN('Please enter name of student', count);
read(studname[count]);
WRITE(studinfo, studname[count]);
WRITELN('Please enter mark of student', count, 'out of 100 (nearest whole number)');
read(studmark[count]); write(studinfo, studmark[count]);
IF studmark[count] >=band6 THEN
WRITELN(studinfo, 'band6');
IF studmark[count] >=band5 THEN
WRITELN(studinfo, 'band5');
IF studmark[count] >=band4 THEN
WRITELN(studinfo, 'band4');
IF studmark[count] >=band3 THEN
WRITELN(studinfo, 'band3');
IF studmark[count] >=band2 THEN
WRITELN(studinfo, 'band2');
IF studmark[count] <band2 THEN
WRITELN(studinfo, 'band1');
IF studmark[count] >= highmark THEN
highmark := studmark[count];
IF studmark[count] <= lowmark THEN
lowmark := studmark[count];
END;
median := highmark MOD 2;
CLOSE(studinfo);
END.
看看你的台词:
While flag = false DO
ReadLN (input);
Flag 永远不会为 false,因此它会永远读取。您的 begin/end 块有问题。
像这样的问题经常会引起评论,例如“SO is not a homework-doing
服务。”这个问题有点特别,因为我想不出有礼貌的方式
的来形容它。它具有由某人写的所有迹象
真的不知道他们在做什么 - 但不要绝望,我们都曾经是初学者!
我不会为你重写你所有的代码——因为你不会从中学到任何东西——但下面至少应该给出
你是一个功能性的主循环,然后你可以自己完成并根据需要进行修饰。
我修复的主要问题是循环中的动作顺序——原来的
委婉地说,完全是一团糟。我添加了用大括号 {} 括起来的各种注释。
最近有一种评论风格使用//但我不知道是什么风格
您正在使用的 Pascal。
代码:
const
band6 = 90;
band5 = 80;
band4 = 70;
band3 = 60;
band2 = 50;
ArraySize = 20; { to set explicit array sizes }
var
studname : array[1..ArraySize] of string;
studmark : array[1..ArraySize] of integer;
flag : boolean;
studinfo : text;
kbdinput : string; { to avoid name clash with program Input param}
count : integer;
num : integer;
{input2: integer; not used}
highmark,
lowmark : integer;
median, average : integer;
begin
lowmark := 100;
highmark := 0;
median := 0;
assign (studinfo, 'c:\temp\examresults.txt'); { always use full path for file name }
Rewrite(studinfo); { set studinfo in the state to allow writing to the file}
flag := false;
writeln('welcome to the band generator.');
writeln('to enter student results, please enter the number of students when prompted.');
while flag = false do
begin
write('please enter a valid number of students. ');
readln(num);
for count := 1 to num do { not change of start and stop values}
begin
write('please enter name of student #', count, ' followed by [Enter] ');
readln(studname[count]);
write(studinfo, studname[count]);
write('please enter mark of student #', count, ' out of 100 (nearest whole number) followed by [Enter] ');
readln(studmark[count]);
write(studinfo, studmark[count]);
end;
{
if studmark[count] >=band6 then
writeln(studinfo, 'band6');
if studmark[count] >=band5 then
writeln(studinfo, 'band5');
if studmark[count] >=band4 then
writeln(studinfo, 'band4');
if studmark[count] >=band3 then
writeln(studinfo, 'band3');
if studmark[count] >=band2 then
writeln(studinfo, 'band2');
if studmark[count] <band2 then
writeln(studinfo, 'band1');
if studmark[count] >= highmark then
highmark := studmark[count];
if studmark[count] <= lowmark then
lowmark := studmark[count];
end;
median := highmark mod 2;
}
writeln('to see class statistics, please type zzz. to clear the screen type zzz and to exit the program, type exit');
readln (kbdinput);
if kbdinput = 'zzz' then
{ The following does nothing useful
while not eof(studinfo) do
begin
writeln(studinfo);
end;
}
;
if kbdinput = 'exit' then
flag := true
else
if kbdinput = 'clear screen' then
{clrscr;'}
close(studinfo);
end;
end.
我的代码有问题。它开始很好,但是当开始要求从用户那里获取输入时,它不会移动到 if 语句。我怎样才能解决这个问题。我试过堆来解决这个问题并最终让它读取数据但不断说它无效。
PROGRAM AT3 (input, output);
uses crt, math;
CONST
band6 = 90;
band5 = 80;
band4 = 70;
band3 = 60;
band2 = 50;
VAR
Studname : array of string;
studmark : array of integer;
flag : boolean;
studinfo : text;
input : string;
count : integer;
num : integer;
input2: integer;
highmark, lowmark : integer;
median, average : integer;
BEGIN
lowmark := 100;
highmark := 0;
median := 0;
ASSIGN (Studinfo, 'ExamResults.txt');
flag := false;
WRITELN('welcome to the Band generator.');
WRITELN('To enter student results, please enter the number of students. To see class statistics, please type zzz. To clear the screen type clear screen. And to exit the program, type exit');
While flag = false DO
ReadLN (input);
IF input = 'zzz' THEN
WHILE not EOF(Studinfo) DO
BEGIN
WRITELN(studinfo);
END;
IF input = 'exit' THEN
Flag := true;
IF input = 'clear screen' THEN
CLRSCR
ELSE
if input2 <> 0 THEN
num := input2
ELSE
WRITELN('Please enter a valid number.');
FOR count := 0 to num-1 DO
BEGIN
WRITELN('Please enter name of student', count);
read(studname[count]);
WRITE(studinfo, studname[count]);
WRITELN('Please enter mark of student', count, 'out of 100 (nearest whole number)');
read(studmark[count]); write(studinfo, studmark[count]);
IF studmark[count] >=band6 THEN
WRITELN(studinfo, 'band6');
IF studmark[count] >=band5 THEN
WRITELN(studinfo, 'band5');
IF studmark[count] >=band4 THEN
WRITELN(studinfo, 'band4');
IF studmark[count] >=band3 THEN
WRITELN(studinfo, 'band3');
IF studmark[count] >=band2 THEN
WRITELN(studinfo, 'band2');
IF studmark[count] <band2 THEN
WRITELN(studinfo, 'band1');
IF studmark[count] >= highmark THEN
highmark := studmark[count];
IF studmark[count] <= lowmark THEN
lowmark := studmark[count];
END;
median := highmark MOD 2;
CLOSE(studinfo);
END.
看看你的台词:
While flag = false DO
ReadLN (input);
Flag 永远不会为 false,因此它会永远读取。您的 begin/end 块有问题。
像这样的问题经常会引起评论,例如“SO is not a homework-doing 服务。”这个问题有点特别,因为我想不出有礼貌的方式 的来形容它。它具有由某人写的所有迹象 真的不知道他们在做什么 - 但不要绝望,我们都曾经是初学者!
我不会为你重写你所有的代码——因为你不会从中学到任何东西——但下面至少应该给出 你是一个功能性的主循环,然后你可以自己完成并根据需要进行修饰。
我修复的主要问题是循环中的动作顺序——原来的 委婉地说,完全是一团糟。我添加了用大括号 {} 括起来的各种注释。 最近有一种评论风格使用//但我不知道是什么风格 您正在使用的 Pascal。
代码:
const
band6 = 90;
band5 = 80;
band4 = 70;
band3 = 60;
band2 = 50;
ArraySize = 20; { to set explicit array sizes }
var
studname : array[1..ArraySize] of string;
studmark : array[1..ArraySize] of integer;
flag : boolean;
studinfo : text;
kbdinput : string; { to avoid name clash with program Input param}
count : integer;
num : integer;
{input2: integer; not used}
highmark,
lowmark : integer;
median, average : integer;
begin
lowmark := 100;
highmark := 0;
median := 0;
assign (studinfo, 'c:\temp\examresults.txt'); { always use full path for file name }
Rewrite(studinfo); { set studinfo in the state to allow writing to the file}
flag := false;
writeln('welcome to the band generator.');
writeln('to enter student results, please enter the number of students when prompted.');
while flag = false do
begin
write('please enter a valid number of students. ');
readln(num);
for count := 1 to num do { not change of start and stop values}
begin
write('please enter name of student #', count, ' followed by [Enter] ');
readln(studname[count]);
write(studinfo, studname[count]);
write('please enter mark of student #', count, ' out of 100 (nearest whole number) followed by [Enter] ');
readln(studmark[count]);
write(studinfo, studmark[count]);
end;
{
if studmark[count] >=band6 then
writeln(studinfo, 'band6');
if studmark[count] >=band5 then
writeln(studinfo, 'band5');
if studmark[count] >=band4 then
writeln(studinfo, 'band4');
if studmark[count] >=band3 then
writeln(studinfo, 'band3');
if studmark[count] >=band2 then
writeln(studinfo, 'band2');
if studmark[count] <band2 then
writeln(studinfo, 'band1');
if studmark[count] >= highmark then
highmark := studmark[count];
if studmark[count] <= lowmark then
lowmark := studmark[count];
end;
median := highmark mod 2;
}
writeln('to see class statistics, please type zzz. to clear the screen type zzz and to exit the program, type exit');
readln (kbdinput);
if kbdinput = 'zzz' then
{ The following does nothing useful
while not eof(studinfo) do
begin
writeln(studinfo);
end;
}
;
if kbdinput = 'exit' then
flag := true
else
if kbdinput = 'clear screen' then
{clrscr;'}
close(studinfo);
end;
end.