วันศุกร์ที่ 6 กรกฎาคม พ.ศ. 2550

Homework3

สรุปเนื้อหาที่เรียนไปวันนี้


คอนแรกอาจารย์ก็สอนเรื่องคลาส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{


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"); }}
จบการเรียนของวันนี้
ตัวดำเนินการ
Relational Operator





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 ความคิดเห็น:

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

จากตัวอย่าง
Double m=in.nextDouble();

ไม่ทราบว่าด้วยเหตุผลใดจึงใช้คลาส Double ในการรับข้อมูล

Giffy ^o^ Ja กล่าวว่า...

ที่ใช้ Double เพราะเวลาลงคะแนน อาจารย์บางวิชาอาจให้คะแนนเด็กนักเรียนเป็นทศนิยม ดังนั้นจึงใช้คลาส Double คะ