ช่วยดูทีครับ ผู้มีความรู้ทั้งหลาย

คือ อ. สั่งงานมาให้ผมทำแต่ผมอ่านแล้ว  ลองแล้วก็ทำไม่ได้ (ผมไม่เข้าใจ*-*)

Program demo6_4;
uses crt;
var  c : byte;
    s,t : real;
    na : string[30];
const line='====================================';
begin
    clrscr;
    Writeln(' *** Report Tax ***');
    Writeln(line);
    Write('Code='); readln(c);
    Write('Name='); readln(na);
    Write('Salary'); readln(s);
    If s > 50000 then t :=s*10/100
    Else if s >= 40000 then t:=s*8/100
    Else if s >= 30000 then t:=s*6/100
    Else if s >= 20000 then t:=s*4/100
    Else if s >= 10000 then t:=s*2/100
    Else t :=s*0/100;
    Write('Tax :=',t:10:2);
    Writeln(line);

End.

โจทร์  จากโปรแกรมนี้ จงเขียนขึ้นใหม่โดยใช้คำสั่ง case แทนคำสั้ง if

(ผมว่าผมพิมโปรแกรมถูกแล้วแต่มันก็รันแล้วตอบไม่ไป  กรุณาช่วยผมทีครับ)

 
🏷️ หัวข้อที่เกี่ยวข้อง

💬 การตอบกลับ 10

#1
Moshi
Moshi
16,996 โพสต์
Program it4x;
uses crt;
var  c : byte;
    s : longint;
    t : real;
    na : string[30];
const line='====================================';
begin
    clrscr;
    Writeln(' *** Report Tax ***');
    Writeln(line);
    Write('Code='); readln(c);
    Write('Name='); readln(na);
    Write('Salary'); readln(s);
    case s of
    0..9999 : t :=s*0/100;
    10000..19999: t :=s*2/100;
    20000..29999: t :=s*4/100;
    30000..32000: t :=s*6/100;
    else
    t :=8*2/100;end;
    Writeln('Tax :=',t:1:2);
    Writeln(line);
    readln;
end.
#2
freedom008
freedom008
11 โพสต์
ขอคุณมากๆเลยครับ *-*  และวันนี้ ก็มีมาีอีกข้อ ต้องขอรบกวนอีกละครับ

Program sem6_13;
Uses crt;
Var  height,base,redius,width,length,side,area:real;
    ans:char;

Begin
    clrscr;
    writeln('Menu selection for calculate the area');
    writeln;
    writeln('1.Triangle.');
    writeln('2.Circle.');
    writeln('3.Rectangln.');
    writeln('4.Square.');
    write('Select =====>'); ans:=readkey;
    writeln;
    case ans of
         '1':begin
                  write('base ='); readln(base);
                  write('height ='); readln(height);
                  area:=0.5*base*height;
                  writeln('Area of triangle =',area:6:2);
             end;


          '2':begin
                   write('radius ='); readln(redius);
                   area:=pi*redius*redius;
                   writeln('Area of circle +',area:6:2);
              end;


          '3':begin
                   write('width ='); readln(width);
                   write('length ='); readln(length);
                   area:=length*width;
                   writeln('Area of rectangle =',area:6:2);
              end;


          '4':begin
                   write('side of square ='); readln(side);
              area:=side*side;
              writeln('Area of square =',area:6:2);
              end;
           end;{Case}
           writeln(#07);
           readln;
End.


อันนี้เป็นข้อสอบก่อนเรียน -*- โจทร์มีอยู่ว่า  
 ให้พัฒนาโปรนี้อันนี้ให้ ดียิ่งขึ้น คือ เวลา ที่โปรแกรมมันให้เราเลือก กด 1 2 3 4 ถ้าเรากดเลือก 1 2 3 4  ก็จะเลือกได้ แต่ถ้าเรากดนอกเหนือจากนั้นโปรแกรมมันจะ ไม่ทำงานและเด้ง
  ผมจึงคิดว่าจะพัฒนาโปรแกรมนี้โดย หาก ผู้เลือก เลือกกดนอกเหนือจาก 1 2 3 4  โปรแกรมจะขึ้น คำว่า "Please select. 1 2 3 4 only"
แต่ปัญหาอยู่ที่ว่า ผมยังไม่ได้เรียน แล้วเขียนไม่เป็น จึงต้องรบกวนท่านๆ และพี่ แอดมินด้วยครับ

✏️ แก้ไขล่าสุด: 30 มกราคม 2553

#3
Moshi
Moshi
16,996 โพสต์
Program sem6_13;
Uses crt;
Var  height,base,redius,width,length,side,area:real;
    ans:char;

Begin
    clrscr;
    writeln('Menu selection for calculate the area');
    writeln;
    writeln('1.Triangle.');
    writeln('2.Circle.');
    writeln('3.Rectangln.');
    writeln('4.Square.');
    write('Select =====>'); ans:=readkey;
    writeln;
    case ans of
          '1':begin
                  write('base ='); readln(base);
                  write('height ='); readln(height);
                  area:=0.5*base*height;
                  writeln('Area of triangle =',area:6:2);
              end;


          '2':begin
                    write('radius ='); readln(redius);
                    area:=pi*redius*redius;
                    writeln('Area of circle +',area:6:2);
              end;


          '3':begin
                    write('width ='); readln(width);
                    write('length ='); readln(length);
                    area:=length*width;
                    writeln('Area of rectangle =',area:6:2);
              end;
          '4':begin
                    write('side of square ='); readln(side);
              area:=side*side;
              writeln('Area of square =',area:6:2);
              end;
          else
          writeln('Please select. 1 2 3 4 only');
            end;{Case}
            writeln(#07);
            readln;
End.
#4
freedom008
freedom008
11 โพสต์
ขอบคุณมากๆครับ แต่  เวลาผู้เลือก เลือกนอกจาก 1 2 3 4 แล้ว มันขึ้น('Please select. 1 2 3 4 only') ก็จริง
แต่มันจะกลับไปทำงานไม่ได้อะครับ ผมอยากให้ขึ้น  ('Please select. 1 2 3 4 only')  แล้วกลัว ไปทำงานที่เลือก 1 2 3 4
อีกครั้งอะครับ ต้องใช้คำสั้ง label  และ goto ใช่ไหมครับ ท่านแอดมิน ลองทำให้ดูหน่อยครับ ผมยังไม่ได้เรียน  label  กับ goto
                                                                                      ขอบคุณครับ
#5
Moshi
Moshi
16,996 โพสต์
Program sem6_13;
Uses crt;
Var  height,base,redius,width,length,side,area:real;
    ans:char;

Begin
    clrscr;
    writeln('Menu selection for calculate the area');
    writeln;
    writeln('1.Triangle.');
    writeln('2.Circle.');
    writeln('3.Rectangln.');
    writeln('4.Square.');
    write('Select =====>'); ans:=readkey;
    writeln;
repeat
    case ans of
          '1':begin
                  write('base ='); readln(base);
                  write('height ='); readln(height);
                  area:=0.5*base*height;
                  writeln('Area of triangle =',area:6:2);
              end;


          '2':begin
                    write('radius ='); readln(redius);
                    area:=pi*redius*redius;
                    writeln('Area of circle +',area:6:2);
              end;


          '3':begin
                    write('width ='); readln(width);
                    write('length ='); readln(length);
                    area:=length*width;
                    writeln('Area of rectangle =',area:6:2);
              end;
          '4':begin
                    write('side of square ='); readln(side);
              area:=side*side;
              writeln('Area of square =',area:6:2);
              end;
          else
          writeln('Please select. 1 2 3 4 only');
            end;{Case}
until ans = '1','2','3','4';
            writeln(#07);
            readln;
End.

ถ้ามัน ERROR ยังไงก็ลองแก้ๆดูเอานะครับ พอดีช่วงนี้งานยุ่งมาก
#6
freedom008
freedom008
11 โพสต์
ท่าน แอทมินครับ มัน  ERROR ตรง ; ซื่งผมก็ไม่รู้จะแก้ยังไงครับ ลง งมๆ เปลี่ยนดูแล้วก็ไม่ไดครับ สงสัยจะเป็นตรงคำสั้งใหม่ที่แอดมีนเขียงลงอะครับ  ถ้าท่าน แอดมีนว่างก็ดูให้ผมทีนะครับ เดียวผมจะลองงมต่อดูครับ  ขอบคุณครับ
#7
Moshi
Moshi
16,996 โพสต์
— freedom008 link=topic=3479.msg7368#msg7368 date=1264905405

ท่าน แอทมินครับ มัน  ERROR ตรง ; ซื่งผมก็ไม่รู้จะแก้ยังไงครับ ลง งมๆ เปลี่ยนดูแล้วก็ไม่ไดครับ สงสัยจะเป็นตรงคำสั้งใหม่ที่แอดมีนเขียงลงอะครับ  ถ้าท่าน แอดมีนว่างก็ดูให้ผมทีนะครับ เดียวผมจะลองงมต่อดูครับ  ขอบคุณครับ


เอา end; หลัง case ลบทิ้งมั้งคับ
#8
freedom008
freedom008
11 โพสต์
ก็ยังไม่ได้อ่ะครับ มันน่าจะเป็น ตรงนี้ until ans = '1','2','3','4'; มันฟ้องว่า ; ตรงแถวนี้อ่ะครับ
#9
Moshi
Moshi
16,996 โพสต์
Program it4x;
Uses crt;
Var  height,base,redius,width,length,side,area:real;
    ans:char;

Begin

    clrscr;
    repeat
    writeln('Menu selection for calculate the area');
    writeln;
    writeln('1.Triangle.');
    writeln('2.Circle.');
    writeln('3.Rectangln.');
    writeln('4.Square.');
    write('Select =====>'); ans:=readkey;
    writeln;
    case ans of
          '1':begin
                  write('base ='); readln(base);
                  write('height ='); readln(height);
                  area:=0.5*base*height;
                  writeln('Area of triangle =',area:6:2);
              end;


          '2':begin
                    write('radius ='); readln(redius);
                    area:=pi*redius*redius;
                    writeln('Area of circle +',area:6:2);
              end;


          '3':begin
                    write('width ='); readln(width);
                    write('length ='); readln(length);
                    area:=length*width;
                    writeln('Area of rectangle =',area:6:2);
              end;
          '4':begin
                    write('side of square ='); readln(side);
              area:=side*side;
              writeln('Area of square =',area:6:2);
              end;
          else
          begin
          clrscr;
          writeln('Please select. 1 2 3 4 only');end;
            end;{Case}
until (ans = '1') or (ans = '2') or (ans = '3') or (ans = '4');
            writeln(#07);
            readln;
End.
#10
freedom008
freedom008
11 โพสต์
ขอบคุณมากกกกครับ