คอนแรกอาจารย์ก็สอนเรื่องคลาสMath
เช่น Math.PI,Math.E,Math.min(),Math.max(),Math.round(),Math.floor(),Math.celi(),Math.random(),Math.pow()
ข้อแตกต่างระหว่าง Math.round() ,Math.ceil()และMath.floor() คือ
- Math.round() //ปัดเศษขึ้นลง --->ได้ผลลัพธ์เป็น int
- Math.ceil() //ปัดเศษขึ้น--->ได้ผลลัพธ์เป็น Double
- Math.floor() //ปัดเศษลง --->ได้ผลลัพธ์เป็น Double
อาจารย์ก็ให้สร้างโปรแกรมรับข้อมูลทางคีบอร์ด คำนวนพื้นที่วงกลม
import java.util.*;
public class circle{
public static void main(String[]args){
Scanner in=new Scanner(System.in);
System.out.print("input radian: ");
int x=in.nextInt();
Double y ; y = Math.PI*x*x;
System.out.println("Area :"+y );
}
}
อันที่2 เป็นการทดลองใช้ Math
public class circle{
public static void main(String[]args){
System.out.println(Math.round(7.61));
System.out.println(Math.round(7.5));
System.out.println(Math.round(7.4));
System.out.println(Math.round(8.2));
System.out.println(Math.round(8.215));
System.out.println(Math.floor(8.9));
System.out.println(Math.ceil(9.5));
System.out.println(Math.ceil(9.9));
System.out.println(Math.ceil(9.215));
}
}
ผลลัพธ์ที่ได้ คือ
8
8
7
8
8
8.0
10.0
10.0
อันที่3
public class circle{
อาจารย์แนะนำเวปด้วย http://java.sun.com/javase/6/docs/api/java/lang/Math.html .ให้ลองเข้าไปดู
อันนี้ก่อนกลับบ้าน อาจารย์สั่งให้เขียนโปรแกรมนี้ ถ้าใครเขียนเสร็จก็กลับบ้านได้
import java.util.*;
public static void main(String[]arg){
Scanner in=new Scanner(System.in);
Logical Operator
ลำดับการประมวลผลของตัวดำเนินการต่างๆ เริ่มจากก่อนไปหลัง
เขียนโปรแกรม 1 โปรแกรมโดยใช้ความรู้ที่เรียนไปในวันนี้
โปรแกรมคำนวนเกรดนักเรียน
import java.util.*;
public class grade1{
public static void main(String[]arg){
Scanner in=new Scanner(System.in);
System.out.print("Input your score of Math <0-100> : ");
Double m=in.nextDouble();
System.out.print("Input your score of Biology <0-100> : ");
Double n=in.nextDouble();
System.out.print("Input your score of Chemisty <0-100> : ");
Double o=in.nextDouble();
System.out.print("Input your score of Physic <0-100> : ");
Double p=in.nextDouble();
System.out.print("Input your score of English <0-100> : ");
Double q=in.nextDouble();
System.out.print("Input your score of Thai <0-100> : ");
Double r=in.nextDouble();
Double sum; sum =(m+n+o+p+q+r)/6;
System.out.println("===========================================");
System.out.println("Your Average score: "+sum);
if(sum>=80)
System.out.println("You get 'A'่");
else if(sum>=70)
System.out.println("You get 'B'่");
else if(sum>=60)
System.out.println("You get 'C'");
else if(sum>=50)
System.out.println("You get 'D'");
else { System.out.println("You get 'F'");
System.out.println("Take Course Again"); }
System.out.println("Bye Bye"); System.out.println
("===========================================");
}}
จบHomework3
2 ความคิดเห็น:
จากตัวอย่าง
Double m=in.nextDouble();
ไม่ทราบว่าด้วยเหตุผลใดจึงใช้คลาส Double ในการรับข้อมูล
ที่ใช้ Double เพราะเวลาลงคะแนน อาจารย์บางวิชาอาจให้คะแนนเด็กนักเรียนเป็นทศนิยม ดังนั้นจึงใช้คลาส Double คะ
แสดงความคิดเห็น