Array
Array จะช่วยในการเก็บข้อมูลให้ง่ายขึ้นได้
เบื้องต้น array จะคล้ายการคิบแบบ matrix
ตัวอย่างคำสั่ง
public class array{
public static void main(String[]args){
Scanner a = new Scanner(System.in);
int [] num;
num = new int [16];
int total = 0;
for( int i = 0; i <16;>
System.out.print("Please input number: ");
num [i] = a.nextInt();
total = total + num[i] ;
}
for( int i = 0; i <>
System.out.println(num[i]);
}
System.out.println(total);
}
}
จากตัวอย่าง
เราจะป้อนข้อมูล 16 ตัวเข้าไปในระบบ
ระบบจะแสดงผลข้อมูลออกมา และทำการรวมข้อมูล
การใช้ loop
for( int i = n; i <>
n = ข้อมูลเริ่มต้น
m = ข้อมูลสุดท้าย
i++ เพิ่มลำดับข้อมูลทีละ 1
i-- ลบลำดับข้อมูลทีละ 1
Array จะช่วยในการเก็บข้อมูลให้ง่ายขึ้นได้
เบื้องต้น array จะคล้ายการคิบแบบ matrix
ตัวอย่างคำสั่ง
public class array{
public static void main(String[]args){
Scanner a = new Scanner(System.in);
int [] num;
num = new int [16];
int total = 0;
for( int i = 0; i <16;>
System.out.print("Please input number: ");
num [i] = a.nextInt();
total = total + num[i] ;
}
for( int i = 0; i <>
System.out.println(num[i]);
}
System.out.println(total);
}
}
จากตัวอย่าง
เราจะป้อนข้อมูล 16 ตัวเข้าไปในระบบ
ระบบจะแสดงผลข้อมูลออกมา และทำการรวมข้อมูล
การใช้ loop
for( int i = n; i <>
n = ข้อมูลเริ่มต้น
m = ข้อมูลสุดท้าย
i++ เพิ่มลำดับข้อมูลทีละ 1
i-- ลบลำดับข้อมูลทีละ 1