ช่วยแก้ code ให้หน่อยครับ

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: ช่วยแก้ code ให้หน่อยครับ

Re: ช่วยแก้ code ให้หน่อยครับ

โดย jataz2 » 01/08/2011 11:20 am

ถ้าจริงๆเเล้ว ต้องการเเบบนี้

โค้ด: เลือกทั้งหมด

if(rank==1||rank==2)
    printf("Lower division \n");
else if(rank==3||rank==4)
  printf("Upper division \n");
else if(rank==5)
   printf("Graduate student \n");
else
   printf("Invalid rank \n");
จะแปลงเป็น switch case ได้เเบบนี้ครับ

โค้ด: เลือกทั้งหมด

switch (rank)
{
      case 1:
      case 2:
          printf("Lower division \n");
          break;
      case 3:
      case 4:
          printf("Upper division \n");
          break;
      case 5:
          printf("Graduate student \n");
          break;
      default:
          printf("Invalid rank \n");
          break; 
}

Re: ช่วยแก้ code ให้หน่อยครับ

โดย mindphp » 18/07/2011 3:18 am

ส่วนที่ || ของ if...else ถ้าแปลงเป็น switch ไม่ต้อง break ที่ตัวแรก

ช่วยแก้ code ให้หน่อยครับ

โดย SMWin » 16/07/2011 9:29 am

ช่วยเปลี่ยน if/else statement ไปเป็น switch statement หน่อยครับ
if(rank==1||rank==2)
printf("Lower division \n");
else
{
if(rank==3||rank==4)
printf("Upper division \n");
else
{
if(rank==5)
printf("Graduate student \n");
else
printf("Invalid rank \n");
}
}

ข้างบน