b02 LTHDT Cac Cau Truc Lenh Trong Java

Post on 06-Mar-2015

121 views 5 download

Transcript of b02 LTHDT Cac Cau Truc Lenh Trong Java

Lập trình hướng đối tượng Các cấu trúc lệnh trong Java

Giảng viên: TS. Nguyễn Mạnh HùngHọc viện Công nghệ Bưu chính Viễn thông (PTIT)

2

Nội dung

Các lệnh lựa chọn Các cấu trúc lệnh lặp Làm việc với mảng Nhập dữ liệu từ bàn phím Bài tập Giới thiệu bài tập lớn: ô số sudoku

Các lệnh lựa chọn

4

Các lệnh lựa chọn

if và if... else... if lồng nhau switch

5

If ...

public class Test{public static void main(String args[]){

if(args.length < 1){System.out.println("khong co doi so dong lenh!");

}}

}

Chạy chương trình:>java TestKhong co doi so dong lenh!>java Test 5 AH>

6

If … else ...public class Test{

public static void main(String args[]){if(args.length < 1){

System.out.println("khong co doi so dong lenh!");}else {

System.out.println("so luong doi so: " + args.length);}

}}

Chạy chương trình:>java TestKhong co doi so dong lenh!>java Test 10 5 11So luong doi so: 3

7

If … else … lồng nhaupublic class Test{

public static void main(String args[]){if(args.length < 1)

System.out.println("khong co doi so dong lenh!");else if(args.length < 5)

System.out.println("so luong doi so tu 1 - 4");else if(args.length < 10)

System.out.println("so luong doi so tu 5 - 10");else

System.out.println("so luong doi so > 10");}

}

Chạy chương trình:>java TestKhong co doi so dong lenh!>java Test 10 5 11So luong doi so tu 1 - 4

8

switch

Chạy chương trình:>java Test 5thursday>java Test 10invalid day of week!

public class Test{public static void main(String args[]){

if(args.length > 0){int day = Integer.parseInt(args[0]);switch(day){case 2: System.out.println("monday"); break;case 3: System.out.println("tuesday"); break;case 4: System.out.println("wednesday"); break;case 5: System.out.println("thursday"); break;case 6: System.out.println("friday"); break;case 7: System.out.println("satuday"); break;case 8: System.out.println("sunday"); break;default: System.out.println("invalid day of week!"); break;}

}}}

Các lệnh lặp

10

while ...public class Test{

public static void main(String args[]){int i = 0;while(i < args.length){

System.out.println(args[i]);i++;

}}

}

Chạy chương trình:>java Test 15 A7 Np15A7Np

11

while và breakpublic class Test{

public static void main(String args[]){int i = 0;while(true){

System.out.println(args[i]);i++;if(i >= args.length) break;

}}

}

Chạy chương trình:>java Test 15 A7 Np15A7Np

12

while và continuepublic class Test{

public static void main(String args[]){int i = 0;while(i < 10){

i++;if((i % 2) == 0) continue;System.out.println(String.valueOf(i));

}}

}

Chạy chương trình:>java Test 13579

13

do … whilepublic class Test{

public static void main(String args[]){int i = 0;do{

System.out.println(args[i]);i++;

}while(i < args.length)}

}

Chạy chương trình:>java Test 15 A7 Np15A7Np

14

do … while và breakpublic class Test{

public static void main(String args[]){int i = 0;do{

System.out.println(args[i]);i++;if(i >= args.length) break;

}while(true)}

}

Chạy chương trình:>java Test 15 A7 Np15A7Np

15

do … while và continuepublic class Test{

public static void main(String args[]){int i = 0;do{

i++;if((i % 2) == 0)continue;System.out.println(args[i]);

}while(i < 10)}

}

Chạy chương trình:>java Test 13579

16

for ...public class Test{

public static void main(String args[]){for (int i = 0; i < args.length; i++){

System.out.println(args[i]);}

}}

Chạy chương trình:>java Test 15 A7 Np15A7Np

17

for và breakpublic class Test{

public static void main(String args[]){for (int i = 0; i < args.length; i++){

if(args[i].equals("a7")) break;System.out.println(args[i]);

}}

}

Chạy chương trình:>java Test 15 a7 Np15>java Test A7 NpA7Np

18

for và continuepublic class Test{

public static void main(String args[]){for (int i = 0; i < args.length; i++){

if(args[i].equals("a7")) continue;System.out.println(args[i]);

}}

}

Chạy chương trình:>java Test 15 a7 Np15Np

19

Bài tập

Viết chương trình tìm và in ra màn hình

các bộ số tự nhiên (a,b,c) nhỏ hơn 1000 sao cho:

a2 = b2 + c2

Làm việc với mảng

21

Gán dữ liệu vào mảng (1)public class Test{

public static void main(String args[]){int[] input;for (int i = 0; i < args.length; i++){

input[i] = Integer.parseInt(args[i]);}

}}

Chạy chương trình:>java Test 15 19 150chuyện gì sẽ xảy ra?

22

Gán dữ liệu vào mảng (2)public class Test{

public static void main(String args[]){int[] input = new int[args.length];for (int i = 0; i < args.length; i++){

input[i] = Integer.parseInt(args[i]);}

}}

Chạy chương trình:>java Test 15 19 150

>java Test chuyện gì sẽ xảy ra?

23

Gán dữ liệu vào mảng (3)public class Test{

public static void main(String args[]){int[] input;if(args != null){

input = new int[args.length];for (int i = 0; i < args.length; i++){

input[i] = Integer.parseInt(args[i]);}

}}

}

Chạy chương trình:>java Test 15 19 150

>java Test

Nhập dữ liệu từ bàn phím

25

InputStreamReader

InputStreamReader br = new InputStreamReader(System.in); try { String input = br.readLine(); } catch (IOException e) { System.out.println(e); }

26

Scanner

Scanner scr = new Scanner(System.in); try { String inputStr = scr.readLine();

int inputInt = scr.nextInt(); } catch (IOException e) { System.out.println(e); }

27

BufferedInputStream

try{BufferedInputStream input = new

BufferedInputStream(System.in);byte[] in = new byte[1024];while((input.read(in)) != -1) {

//do something with data...}input.close();

}catch(IOException e){System.out.println(e);

}

28

DataInputStream

try{DataInputStream input = new

DataInputStream(System.in);String in = input.readUTF();//do something with data...input.close();

}catch(IOException e){System.out.println(e);

}

29

Ví dụ (1) // đọc một mảng các số vào từ bàn phím (cùng 1 dòng)

// các số cách nhau bởi dấu trốngInputStreamReader br = new InputStreamReader(System.in);

try {// đọc một dòng từ bàn phím

String input = br.readLine();// tách các số cách nhau bởi dấu trốngString[] tmpStr = input.split(" ");// khởi tạo mảng cần lưu dữ liệuint[] result = new int[tmpStr.length];// gán các số vào mảng kết quả, có chuyển từ String sang intfor (int i = 0; i < tmpStr.length; i++){

result[i] = Integer.parseInt(tmpStr[i]);}

} catch (IOException e) { System.out.println(e); }

30

Ví dụ (2)public class Test{

public static void main(String args[]){InputStreamReader br = new InputStreamReader(System.in);

try { String input = br.readLine();

String[] tmpStr = input.split(" ");int[] result = new int[tmpStr.length];for (int i = 0; i < tmpStr.length; i++){

result[i] = Integer.parseInt(tmpStr[i]);}

} catch (IOException e) { System.out.println(e); }

}}

31

Bài tập

Viết chương trình nhận một ma trận hai

chiều, chứa các số, từ bàn phím

Giới thiệu bài tập lớn:Ô số sudoku

33

Ô số sudoku: mức độ dễ (1)

Source: http://www.sudokukingdom.com/

34

Ô số sudoku: mức độ dễ (2)

Source: http://www.sudokukingdom.com/

35

Ô số sudoku: khó vừa

Source: http://www.sudokukingdom.com/

36

Ô số sudoku: khó

Source: http://www.sudokukingdom.com/

37

Ô số sudoku: rất khó

Source: http://www.sudokukingdom.com/

Questions?