2016-07-28 4 views
0

コードに問題があります。それはうまく始まりますが、開始時にユーザーからの入力を求めるときは、ifステートメントに移動しません。どうしたらいいですか?私はこれを回避するためにヒープを試して、最終的にデータを読み取るようにしましたが、それが無効であると絶えず言います。あなたのラインでコードが過去に移動しないRead

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. 
+0

まさに、 "それは、if文の上に移動しませんか"? Btw、コードには他にもいくつか問題があります。あなたの "Assign(Studinfo、 'ExamResults.txt')"の直後に "Rewrite(Studinfo)"が続くはずです。また、どのバージョンのPascalを使用していますか? – MartynA

+0

ええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええとそれはまた、同じ問題とこの 'マーク/バンド'事を持っていました。大文字のキーワード(ほとんどの場合)と、これも '' zzz'' =プログラムアイデアの終わりです。 –

+0

インデントだけでは、パスカルでブロックが定義されません。マルチステートメントブロックには 'begin'と' end'が必要です。 – lurker

答えて

2

このような質問は、「宿題をしていないので、 サービス」のようなコメントを頻繁に惹きつけます。このqは、私が考えることができる礼儀正しい方法がないので、少し例外的です。 を記述してください。 は、彼らが何をしているのか本当に分かっていない人が書いたことのすべての徴候を持っていますが、私たちは一度すべての初心者でした!

私はあなたのコードをすべて書き直すつもりはありません。なぜなら、そのことから何かを学ばないからです。しかし、少なくとも次のようにすれば、あなた自身が完成し、必要に応じて装飾できる機能的なメインループが得られるはずです。 。

私が修正した主なものは、ループ内のアクションのシーケンスです。オリジナルの は軽度に置くための完全な混乱でした。中括弧{}で囲まれたさまざまなコメントを追加しました。 より最近のスタイルのコメントが//使用されていますが、私はあなたが使っているフレーバー を知りません。

コード:あなたが意味するかif`どの `ステートメント

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. 
1

ルック:それは永遠に読み込むよう

While flag = false DO 
     ReadLN (input); 

フラグがfalseになることはありません。あなたはあなたのbegin/endブロックに問題があります。

+0

'Flag'は常に' false'です。しかし、 'input = 'exit'ならtrueに設定されます。それはループを止めるべきです。 –

+0

@RudyVelthuis OPの投稿されたコードでは、 'if input = 'exit''が' while'ボディのスコープ内にありません。 'while'ボディに' begin'/'end'がありません。 – lurker

+1

@lurker:ああ、コード内の別の誤り。コードが意味をなさないほど多すぎます。 –

関連する問題