วันเสาร์ที่ 28 กรกฎาคม พ.ศ. 2550

6-7-2007 In class JAVA

เรื่องคลาส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
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));
}

ตัวอย่างที่ 3
public class circle{
public static void main(String[]args){
float rand1=(float)Math.random();
float rand2=rand1*10;
int rand3=(int)Math.round(rand2);
System.out.println("Basic random is "+rand1);
System.out.println("Bigger range random is "+rand2);
System.out.println("Rounded up random is "+rand3);
int rand4=(int)Math.ceil(Math.random()*10);
System.out.println("Another Ceiling random is "+rand4);
}
}

อาจารย์แนะนำเวป
http://java.sun.com/javase/6/docs/api/java/lang/Math.html

import java.util.*;

public class score1{
public static void main(String[]arg){
Scanner in=new Scanner(System.in);
System.out.print("Input score1 : ");
Double x=in.nextDouble();
System.out.print("Input score2 : ");
Double y=in.nextDouble();
System.out.print("Input score3 : ");
Double z=in.nextDouble();
Double sum;
sum=x+y+z;
if(sum>=50){
System.out.println("ผ่านและเรียนวิชาใหม่");
System.out.println("Take new Couse");
}
else {
System.out.println("ไม่ผ่าน");
System.out.println("เรียนซ้ำ");
}
System.out.println("Bye Bye");
}
}

credit: http://giffykids.spaces.live.com/

1 ความคิดเห็น:

han chayakan กล่าวว่า...

ควรมีคำอธิบายโปรแกรมด้วยครับ