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

easySwap

import java.util.Scanner;
public class Swapeasy {
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
int amount=scan.nextInt();

int number[]=new int[amount];
int numberreverse[]=new int [amount];

/////////////get number from user //and Swap
int j=amount-1;
for (int i=0; i < amount;i++)
{
number[i]=scan.nextInt();
numberreverse[j]=number[i];
j--;
}
for(int i=0;i < amount;i++)
{
System.out.printf("%d\n",numberreverse[i]);
}
}
}

Swap

import java.util.Scanner;


public class Swap {

public static void main(String args[])

{

Scanner scan=new Scanner(System.in);

int amount=scan.nextInt();



int num[]=new int [amount];

//////////get value from keyboard

for(int i=0; i < num.length ; i++)

{

num[i]=scan.nextInt();

}

//////////////////////////////





/////Swap

int temp;

int i3=amount-1;

for(int i2=0; i2 < num.length/2 ; i2++)

{

temp=num[i2];

num[i2]=num[i3];

num[i3]=temp;

i3--;

}

//////////////////////////////////



//example amount=5;

///int num[]=new num[amount];

// array have 0 1 2 3 4

//give i3=5-1;





///i2 < num.length/2  because for once can swap 2 value

//in this case num.length/2 = 2;

//so , it will for 2 times



//first for i3=4;

//i2=0;



////second for i3=3;

//i2=1;



////i2=2; it will be not change value



///test input 1 2 3 4 5



//first

//1=5

//5=1



//second

//2=4

//4=2



//3 not swap because 3 is mid value

//it will be not change value

////////////////////////////////////









///////////////show value

for(int i4=0; i4 < num.length ; i4++)

{

System.out.printf("%d\n",num[i4]);

}



}

}

วันพุธที่ 16 กันยายน พ.ศ. 2552

การใช้คำสั่งรับค่าจากคีย์บอด

การจะใช้คำสั่งรับค่าจะคีย์บอดอันดับแรกต้องไปเรียกไลบราลี่มาก่อนโดยการพิมพ์ import java.util.Scanner; ที่บรรทัดบนสุดและต้องประกาศตัวแปรแบบจองหน่วยความจำก่อนที่จะใช้Scanner ชื่อตัวแปรที่เราจะตั้ง = new Scanner(System.in);
จากนั้นก็เอาตัวแปรที่เราตั้งรับค่าแบบนี้
int number;
number=ตัวแปรที่เราตั้งชื่อ.nextInt();
การใช้nextจะแล้วแต่ชนิดของตัวแปรที่เราต้องการจะรับ
int ใช้กับ nextInt();String ใช้กับ next();double ใช้กับ nextdouble();

เช่น
import java.util.Scanner;
class test
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
int a;
a=scan.nextInt();
double b;
b=scan.nextDouble();
String c;
c=scan.next();
System.out.printf("%d%f%s",a,b,c);
}
}

การใช้ printf

การใช้คำสั่งprintfเป็นคำสั่งของภาษาซีซึ่งในภาษาจาวาก็สามารถใช้ได้เหมือนกันโดยการเขียน
System.out.printf("Hello");

ในกรณีที่เราต้องการให้ประโยคนึงขึ้นต้นบรทัดใหม่ให้พิมพ์ \nล งในภายในเครื่องหมายคำพูด
ซึ่ง\nมีค่าเท่ากับเรากดEnter 1 ที

ตัวอย่าง
System.out.printf("Hello\nWorld");
ผลที่ออกมา
Hello
World

System.out.printf("A\nB\nC");
A
B
C

และถ้าเราต้องการเอาค่าของตัวแปรมาแสดงก็ต้องใช้ %d %s %f มาใช้
ซึ่งแต่ละชนิดของตัวแปรนั้น จะใช้%ต่างกัน
ตัวแปร byte , short , int , long ใช้%d
ตัวแปร char , String ใช้ %s
ตัวแปร double , float ใช้ %f
System.out.printf(" เอา%ของชนิดที่ตัวแปรที่ต้องการเอามาแสดง ",ตัวแปรที่ต้องการเอามาแสดง);
เช่น

int a=5;
System.out.printf("%d",a);

double b=15.25;
System.out.printf("%f",b);

String c="Hello";
System.out.printf("%s",c);

ผลที่ออกมา
5
15.250000
Hello
สามารถพิมพ์คำสั่งบรรทัดเดียวเลยก็ได้เช่น
int a=5;
double b=15.25;
String c="Hello";
System.out.printf("%d%f%s",a,b,c);


%fถ้าเราต้องการใช้มันโชว์ตัวแปรชนิดdouble float ทษนิยมแค่ 2 หลักจะต้องใช้
%.2f

double b=15.25;
System.out.printf("%.2f",b);

ผลที่แสดงออกมา
15.25

การแสดงค่าของตัวแปรเรายังสามารถใช้ร่วมกันข้อความได้ด้วยเช่น
String a="Pawit";
System.out.printf("Hi , %s\n",a);

Hello World

System.out.printf("Hello World\n");
System.out.println("Hello World");