วันศุกร์ที่ 20 กันยายน พ.ศ. 2556

Variable

Easy

Programming and problem solving white Java Nell Dale and Chip Weems. ISBN-10 : 0-7637-3402-0
page : 101

Question : Write variable declaration and a variable type int, String and variable type float.

แปล : เขียนการประกาศและกำหนดตัวแปรสามชนิดตัวแปรประเภทint ,String  และตัวแปรประเภท float.

Answer    String a="modle";//ประกาศ+กำหนด ตัวแปล a ชนิด string
                  int c=10;//ประกาศ+กำหนด ตัวแปล c ชนิด int เป็นจำนวนเต็ม
                  float e=5.3;//ประกาศ+กำหนด ตัวแปล e ชนิด float เป็นทศนิยม

Medium

Programming and problem solving white Java Nell Dale and Chip Weems. ISBN-10 : 0-7637-3402-0
page : 101

Question : write code to print the following group of text.
old : seven years
 แปล :  เขียนโค้ดที่จะแสดงกลุ่มของข้อความต่อไปนี้
                  old : seven years

code

void setup() {
  size(200, 200);
  background(2);
  String a;   //ประกาศตัวแปล a ชนิด string
  String b="seven years"; // กำหนด ตัวแปล b ชนิด string ให้มีค่าเท่ากับ seven years
  a = b;
  textSize(20);
  text("old : "+a, 20, 100);
}

result




Hard

Java for students second edition. DOUGLAS BELL AND MIKE PARR ISBN : 0-13010922-3

Question : Assume that two student s take a java exam, and the result –as produced by a very discriminating examiner-are assigned to two variables:
float mark1=50.7;
float mark2=55.9;
Write a program which calculates and displays the average mark as a float value. Check your answer with a calculator.

แปล : สมมติว่านักเรียนสองคนใช้เวลาสอบ java และผลลัพธ์ที่ได้ โดยผู้ตรวจได้จำแนกและกำหนดให้เป็นสองตัวแปร:
float mark1 = 50.7;
float mark2 = 55.9;
เขียนโปรแกรมที่จะคำนวณและแสดงผลMarkเป็นค่าเฉลี่ยfloat ตรวจสอบคำตอบของคุณกับเครื่องคิดเลข

code
float a=57.6; //ประกาศ+กำหนดตัวแปล ชนิด float เป็นทศนิยม 
float b=22.9;
float sum; //ประกาศตัวแปล sum
float avr;
int i=0; //ประกาศ+กำหนดตัวแปล ชนิด int  เป็นจำนวนเต็ม
void setup() {
  size(200,200);
  background(2);
  if (i<2) {
    sum=a+b; //เรียกใช้ตัวแปล sum 
  }
  if (a>1) {
    avr=sum/2;
  }
  textSize(20);
  text("average : "+avr, 20, 100);
}

result


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

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