Proposition => Write a function that take an integer n and prints a list of squares and cubes of the number 1-n. From main(), read the number n from the user.
Translated into Thailand =>เขียนฟังก์ชั่นที่ใช้จำนวนเต็ม n และพิมพ์ค่าของจำนวนตั้งแต่ 1-n
Solution=>
int n=5;
int i=1;
void draw() {
Print(); //เรียกใช้ฟังชันPrint( )
}
void Print() {
if (i<=n) {
print(i); // แสดงค่าของจำนวน i
i++;
}
}
Out put => 1 2 3 4 5
You can run code in http://processingjs.org/tools/processing-helper.html
text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
ISBN 978-0-7637-3402-2
Translated into Thailand =>เขียนฟังก์ชั่นที่ใช้จำนวนเต็ม n และพิมพ์ค่าของจำนวนตั้งแต่ 1-n
Solution=>
int n=5;
int i=1;
void draw() {
Print(); //เรียกใช้ฟังชันPrint( )
}
void Print() {
if (i<=n) {
print(i); // แสดงค่าของจำนวน i
i++;
}
}
Out put => 1 2 3 4 5
You can run code in http://processingjs.org/tools/processing-helper.html
text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
ISBN 978-0-7637-3402-2
ไม่มีความคิดเห็น:
แสดงความคิดเห็น