วันจันทร์ที่ 5 สิงหาคม พ.ศ. 2556

Condition

Easy

Programming in C++ / Nell Dale,Chip Weems.
    ISBN : 0-7637-3234-6(pbk.)
    page : 194
Question :  write the Boolean expression that is true when the variable moon has the value "blue" .
แปล : เขียนการแสดงผล Boolean ที่เป็น จริง เมื่อ ตัวแปล moon มีค่าเท่ากับ blue .

code
void setup() {
  boolean a=true;
  boolean b=false;
  int moon=0;
  int blue=1;
  moon=blue;
    if(moon==blue){   //เป็นเงื่อนไขที่ใช้ในการหาค่า ตัวแปล boolean จากโจทย์ ถ้าmoonเท่ากับblue
      println(a);   //จะปริ้นค่า a
    }else{  //ไม่ใช่ก็จะ
      println(b);   //จะปริ้นค่า b
    }

}

result

true  

Medium

Programming in C++ / Nell Dale,Chip Weems.
    ISBN : 0-7637-3234-6(pbk.)
    page : 195
Question :  write the statement for a Boolean expression that is true when variable has value 1 and  is false when variable has value 0 . 
แปล : เขียนคำสั่งในการแสดง Boolean ที่เป็นจริงเมื่อตัวแปรมีค่า 1 และเป็นเท็จเมื่อตัวแปรมีค่า 0.

code
boolean a=true;
boolean b=false;
int c=0;
int d=1;

if(c!=0){  //เป็นเงื่อนไขที่ใช้ในการหาค่า ตัวแปล boolean จากโจทย์ ถ้าc ไม่เท่ากับ 0 
  println(a);  //ปริ้นค่า a
}else{ //ไม่ใช่ ทำ statement นี้
  if(c==0){  //ถ้า c เท่ากับ 0 
   println(a); //ปริ้นค่า a
   c=c+1; //ให้ค่า c เพิ่มขึ้นอีก 1
  }
  if(c>=0){ //ถ้า c มากกว่าเท่ากับ 0
    c=d; // ให้ c เท่ากับ d
    println(b); //ปริ้นค่า b
  }
}

result


true
false

Hard

Programming in C++ / Nell Dale,Chip Weems.
    ISBN : 0-7637-3234-6(pbk.)
    page : 195

Question :  write a  statement that test whether score is in the range of 0 to 100 and output.
แปล : คำสั่งที่ทดสอบ คะแนน ในช่วง 0 - 100 และแสดงออกมา.

code
void setup() {
  int score=85;
  if (score<50) {  //เป็นเงื่อนไขที่ใช้ในการตรวจสอบคะแนนสอบ คือ ถ้า score น้อยกว่า 50
    println("Fail");  //แสดง Fail
  }
  else { // ไม่ใช้ ให้ทำ statement นี้
    if (score<60) { // ถ้า score น้อยกว่า 60
      println("Below Average"); // แสดงBelow Average
    }
    else { // ไม่ใช้ ให้ทำ statement นี้
      if (score<70) { //ถ้า score น้อยกว่า 70
        println("Average"); // แสดง Average
      }
      else { // ไม่ใช้ ให้ทำ statement นี้
        if (score<80) { //ถ้า score น้อยกว่า 80
          println("Average"); // แสดง Average
        }
        else { // ไม่ใช้ ให้ทำ statement นี้
          if (score<90) { //ถ้า score น้อยกว่า 90
            println("very good"); //แสดง very good
          }
          else { // ไม่ใช้ ให้ทำ statement นี้
            if (score<=100) { //ถ้า score น้อยกว่าเท่ากับ 100
              println("Excellent"); // แสดง Excellent
            }
          }
        }
      }
    }
  }

}


result

very good

ทดสอบ code : http://processingjs.org/tools/processing-helper.html

ไม่มีความคิดเห็น:

แสดงความคิดเห็น