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

try{ DataInputStream input= new DataInputStream(System.in); String in= input.readUTF(); //do something with data. input.close(); }catch(IOException e){ System.out.println(e); }

pdf38 trang | Chia sẻ: phanlang | Lượt xem: 1906 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Lập trình hướng đối tượng Các cấu trúc lệnh trong Java, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
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ùng Học viện Công nghệ Bưu chính Viễn thông (PTIT) 2Nộ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 4Các lệnh lựa chọn  if và if... else...  if lồng nhau  switch 5If ... 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 Test Khong co doi so dong lenh! >java Test 5 AH > 6If … 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 Test Khong co doi so dong lenh! >java Test 10 5 11 So luong doi so: 3 7If … else … lồng nhau public 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 Test Khong co doi so dong lenh! >java Test 10 5 11 So luong doi so tu 1 - 4 8switch Chạy chương trình: >java Test 5 thursday >java Test 10 invalid 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 Np 15 A7 Np 11 while và break public 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 Np 15 A7 Np 12 while và continue public 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 1 3 5 7 9 13 do … while public 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 Np 15 A7 Np 14 do … while và break public 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 Np 15 A7 Np 15 do … while và continue public 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 1 3 5 7 9 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 Np 15 A7 Np 17 for và break public 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 Np 15 >java Test A7 Np A7 Np 18 for và continue public 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 Np 15 Np 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 150 chuyệ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ống InputStreamReader 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ống String[] tmpStr = input.split(" "); // khởi tạo mảng cần lưu dữ liệu int[] result = new int[tmpStr.length]; // gán các số vào mảng kết quả, có chuyển từ String sang int for (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: 34 Ô số sudoku: mức độ dễ (2) Source: 35 Ô số sudoku: khó vừa Source: 36 Ô số sudoku: khó Source: 37 Ô số sudoku: rất khó Source: Questions?

Các file đính kèm theo tài liệu này:

  • pdfb02_2829.pdf
Tài liệu liên quan