วันพฤหัสบดีที่ 20 กันยายน พ.ศ. 2550

week8

string ที่มีช่องว่างระหว่าง 1 ตัวขึ้นไปออกโดยวิธีการใช้คือ a.trim()เช่น" hi ".trim()จะได้ผลลัพท์คือ " hi " ออกมาการเปลี่ยนตัวอักษรตัวพิมพ์ใหญ่ เล็กการเปลี่ยนตัวพิมพ์ใช้คำสั่ง .toLowerCase() (ได้ตัวพิมพ์เล็กทั้งหมด) หรือ .toUpperCase (ได้ตัวพิมพ์ใหญ่ทั้งหมด)เช่นString x = "Hi";

System.out.println("x to y : " + x);

String b = a.toLowerCase();

System.out.println("y : " + y);

String z = x.toUpperCase();

System.out.println("z : "+ z);

Resultx before change case : Hi?y : Hi z : HELLO HOW ARE YOU?

การเปรียบเทียบของ stringโดยใช้คำสั่ง equals ถ้ามีตัวใหญ่ต้องใหญ่ s,f และถ้าเล็กก็เล็กเหมือนกันโดยผลที่แสดงออกมาจะเป็น True กับ False

เช่นString x = "Ho" ;String y = x ;String z = "ho" ;

x.equals(x) ;

x.equals(x) ;

x.equals(z) ;จะได้ว่าx, y แสดงค่าเป็น true แต่ z แสดงค่าเป็น false เนื่องจาก H กับ h มีค่าต่างกันในอีกทางหนึ่ง

ในการเปรียบเทียบนั้นค่าจะเท่ากันต่อเมื่อ1. มีค่าเท่ากัน2. ตัวแปรเดียวกันเท่านั้นซึ่งไม่เหมือนกับ equals ซึ่งเอาตัวแปรเท่าใหม่เท่ากับตัวแปรตัวเก่าได้

เช่นString a = "Hello";

System.out.println("x== \"Hi\" is : " + (x == "Hello"));String y = x;

System.out.println("y == x is : " + (y==x));

String c = new String(x);System.out.println("z == x : " + (z==x))

วันศุกร์ที่ 31 สิงหาคม พ.ศ. 2550

week6

เป็นการใช้else if ในการสั่งคำสั่งต่างๆเพื่อที่จะใช้ได้หลายๆคำสั่งแต่ถ้าใช้if else ธรรมดาจะสามารถใช้ได้เพียงแค่2คำสั่ง

x = 150;
if(x==120){
System.out.println("x="+x); System.out.println("condition=true");}
else if(x>120 ){ System.out.println("fail"); System.out.println("it's to large");}
else if(x<120 ){ System.out.println("fail"); System.out.println("it's to low");};
เป็นการใช้คำสั่งelse if ในการดูว่าข้อมูลที่อยู่นั้นมากกว่าหรือน้อยกว่าหรือเท่ากับตค่าที่ตั้งไว้

ส่วนอีกคำสั่งนั้นคือcaseเกี่ยวกับการแยกเงื่อนไขให้ง่ายกว่าการใช้ if else
public class week62 {
public static void main (String args [])
{ int x; x = 100;
switch(x/10)
{
case 0:;
case 1:;
case 2:;
case 3:;
case 4:System.out.println("You're buffullo");break;
case 5:System.out.println("You're normal-.");break;
case 6:System.out.println("You're normal");break;
case 7:System.out.println("You're normal+.");break;
case 8:;
case 9:;
case 10:System.out.println("You're smart. oh! very good");break;
}
}
เกี่ยวกับการแยกข้อมูลถ้าได้สกอร์เท่านี้จะเป็นอย่างไร
ตัวอย่างผลงาน
http://www.megaupload.com/?d=KODIUEK0

week7

เป็นการเรียนเกี่ยวกับarrey
จะเป็นการใช้คำสั่งarreyในการสั่งคำสั่งต่างๆ สมมุติว่าถ้ายังไม่ถึงตัวที่จะใช้งาน คำสั่งนั้นก็จะrunไปเรื่อย แต่พอถึงคำสั่งที่ต้องใช้ทำงานแล้ว คำสั่งจะจบทันที
public class week71 {
public static void main (String[]args){
int i , x[] , y[] , z[] ; String n[] ;
x = new int[3] ;
x[0] = 50 ;
x[1] = 79 ;
x[2] = 78 ;
y = new int[3] ;
y[0] = 80 ;
y[1] = 65 ;
y[2] = 79 ;
z = new int[3] ;
z[0] = 50 ;
z[1] = 65 ;
z[2] = 80 ;
n = new String[3] ;
n[0] = "GaMsT" ;
n[1] = "RoBbEn" ;
n[2] = "LaMpArD" ;
for(i = 0 ; i < 3 ; i++){
System.out.println("Full name is "+ n[i] +". First test score is "+ x[i] +" points. Second test score is "+ y[i] + " points. Third test score is " + z[i] +" points.");
System.out.println("The average score of "+ n[i] +" is "+ ((x[i] + y[i] + z[i])/3) +" points.");
if(x[i]<=y[i]) if(x[i]<=z[i])
System.out.println("lowest score "+ n[i] +" is "+ x[i] +" points."); else if(y[i]<=x[i]) if(y[i]<=z[i])
System.out.println("lowest score "+ n[i] +" is "+ y[i] +" points."); else if(z[i]<=x[i]) if(z[i]<=y[i])
System.out.println("lowest score "+ n[i] +" is "+ z[i] +" points."); if(x[i]>=y[i]) if(x[i]>=z[i])
System.out.println("highest score "+ n[i] +" is "+ x[i] +" points."); else if(y[i]>=x[i]) if(y[i]>=z[i])
System.out.println("highest score "+ n[i] +" is "+ y[i] +" points."); else if(z[i]>x[i]) if(z[i]>y[i])
System.out.println("highest score "+ n[i] +" is "+ z[i] +" points.");
System.out.println(); } } }
ตัวอย่างผลงาน
http://www.megaupload.com/?d=Q11M1ZVW

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

งานสัปดาห์ที่5

import java.util.*;public class workplay {
public static void main (String [] args ) {
Scanner a = new Scanner (System.in);
Float x;
x = a.nextFloat();
if (x==120)
System.out.println("jackpot");
else
System.out.println("fail");
}
}

สัปดาห์นี้เกี่ยวกับการมีเงื่อนไขในการใช้งาน สมมุติว่าถ้าตัวเลขที่กรอกลงไปแล้วมันไม่ใช่กับตัวที่เราตั้งไว้ จะข้นว่า failแต่ถ้าตรงกับเลขที่ตั้งไว้จะข้นว่าjackpot

วันพฤหัสบดีที่ 19 กรกฎาคม พ.ศ. 2550

งานของสับดาห์ที่4

import java.util.*;public class xyz {
public static void main (String [] args )
{ Scanner a = new Scanner (System.in);
Scanner b = new Scanner (System.in);
Scanner c = new Scanner (System.in);
float x;
float y;
float z;
System.out.print("score1="); x = a.nextFloat();
System.out.print("score2="); y = b.nextFloat();
System.out.print("score3="); z = c.nextFloat();
System.out.println("score1= " +x);
System.out.println("score2= " +y);
System.out.println("score3= " +z);
System.out.println (("sumscore")+(x+y+z));
System.out.println (("averate")+(x+y+z)/3);
}
}
อันนี้เป็นตัวอย่างการทำงาน



import java.util.*;public class weekfour {
public static void main (String [] args )
{ Scanner a = new Scanner (System.in);
Float x;
x = a.nextFloat();
System.out.println(x);
System.out.println ("score1"+"="+ Integer.parseInt(args[0]));
System.out.println ("score2"+"="+ Integer.parseInt(args[1]));
System.out.println ("score3"+"="+ Integer.parseInt(args[2]));
System.out.println ((Integer.parseInt(args[0]))+(Integer.parseInt(args[1]))+(Integer.parseInt(args[2])));
System.out.println ((Integer.parseInt(args[0]))+(Integer.parseInt(args[1]))+(Integer.parseInt(args2]))/3.0);

}
}
อันนี้คืองานสับด่ห์ที่4

วันพฤหัสบดีที่ 28 มิถุนายน พ.ศ. 2550

week3

สัปดาห์ที่3เรียนรู้ถึงการคำนวนเลขของโปรแกรมจาวา ซึ่งเอาตัวเลข2จำนวนมา+กันและสามารถที่จะกรอกข้อมูลอะไรก้อได้ลงไปแล้วนำมาคำนวนได้เอง



public class sumscore {


public static void main (String args [] ) {


System.out.println (args[ 0 ]); System.out.println ("score1"+"="+ Integer.parseInt(args[1])); System.out.println ("score2"+"="+ Integer.parseInt(args[2]));


System.out.println (Integer.parseInt(args[1])+Integer.parseInt(args[2]));

} }


ผลที่ออกมาจะได้แบบนี้

โหลดงานได้ที่ http://www.megaupload.com/?d=CXZW0294

week2

สัปดาห์นี้เรียนรู้เกี่ยวกับการคำนวนตัวเลขของโปรแกรม จาวา ซึ่งปกติแล้วการคำนวนของโปรแกรมจาวานี้นจะไม่แสดงเลขทศนิยม แต่จะแสดงเป็นตัวเลขที่เป็นหลักเช่น หลักหน่วย หลักสิบ หลักร้อย ดังนั้นเราจึงต้องใส่จุดทศนิยมเพื่อให้โปรแกรมนั้นคำนวนเป็นทศนิยม ถ้าไม่ใส่ การคิดจะออกมาเป็นจำนวนเต็ม แต่พอใส่จุดทศนิยมให้นั้นจะทำให้การคำนวนออกมาเป็นทศนิยม

public class first {
public static void main (String[] args){
System.out.println("Rectangle area = 1/2 * base * height"); System.out.println("Base = " +10 );
System.out.println("Height = " +20);
System.out.println("Rectangle area ="+( 1.0/2*10* 20));
}
}