Bài giảng lập trình hướng đối tượng các cấu trúc lệnh trong java TS nguyễn mạnh hùng

38 447 0
Bài giảng lập trình hướng đối tượng  các cấu trúc lệnh trong java   TS  nguyễn mạnh hùng

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Lập trình hướng đối tượng Các cấu trúc lệnh Java Giảng viên: TS Nguyễn Mạnh Hùng Học viện Công nghệ Bưu Viễn thông (PTIT) 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 liệu từ bàn phím Bài tập Giới thiệu tập lớn: ô số sudoku Các lệnh lựa chọn Các lệnh lựa chọn    if if else if lồng switch 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 Test Khong co doi so dong lenh! >java Test AH > 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 Test Khong co doi so dong lenh! >java Test 10 11 So luong doi so: If … else … lồng 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 - 4"); else if(args.length < 10) System.out.println("so luong doi so tu - 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 11 So luong doi so tu - switch 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; } } }} Chạy chương trình: >java Test thursday >java Test 10 invalid day of week! Các lệnh lặp 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 10 Nhập liệu từ bàn phím InputStreamReader InputStreamReader br = new InputStreamReader(System.in); try { String input = br.readLine(); } catch (IOException e) { System.out.println(e); } 25 Scanner Scanner scr = new Scanner(System.in); try { String inputStr = scr.readLine(); int inputInt = scr.nextInt(); } catch (IOException e) { System.out.println(e); } 26 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); } 27 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); } 28 Ví dụ (1) // đọc mảng số vào từ bàn phím (cùng dòng) // số cách dấu trống InputStreamReader br = new InputStreamReader(System.in); try { // đọc dòng từ bàn phím String input = br.readLine(); // tách số cách dấu trống String[] tmpStr = input.split(" "); // khởi tạo mảng cần lưu liệu int[] result = new int[tmpStr.length]; // gán 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); } 29 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); } } } 30 Bài tập  Viết chương trình nhận ma trận hai chiều, chứa số, từ bàn phím 31 Giới thiệu tập lớn: Ô số sudoku Ô số sudoku: mức độ dễ (1) Source: http://www.sudokukingdom.com/ 33 Ô số sudoku: mức độ dễ (2) Source: http://www.sudokukingdom.com/ 34 Ô số sudoku: khó vừa Source: http://www.sudokukingdom.com/ 35 Ô số sudoku: khó Source: http://www.sudokukingdom.com/ 36 Ô số sudoku: khó Source: http://www.sudokukingdom.com/ 37 Questions? [...]... System.out.println(args[i]); } } } Chạy chương trình: >java Test 15 a7 Np 15 >java Test A7 Np A7 Np 17 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 18 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... 28 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ó... input[i] = Integer.parseInt(args[i]); } } } } Chạy chương trình: >java Test 15 19 150 >java Test 23 Nhập dữ liệu từ bàn phím InputStreamReader InputStreamReader br = new InputStreamReader(System.in); try { String input = br.readLine(); } catch (IOException e) { System.out.println(e); } 25 Scanner Scanner scr = new Scanner(System.in); try { String inputStr = scr.readLine(); int inputInt = scr.nextInt();... (IOException e) { System.out.println(e); } 26 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); } 27 DataInputStream try{ DataInputStream input = new DataInputStream(System.in); String in = input.readUTF(); //do something... input[i] = Integer.parseInt(args[i]); } } } Chạy chương trình: >java Test 15 19 150 chuyện gì sẽ xảy ra? 21 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? 22 Gán dữ liệu vào mảng... 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); } } } 30 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... 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 11 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 12 do … while public class Test{ public static void main(String... 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 13 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 14 do … while và continue public class Test{ public static... 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 15 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 16 for và break public class Test{ public static void main(String args[]){ for... i++){ result[i] = Integer.parseInt(tmpStr[i]); } } catch (IOException e) { System.out.println(e); } } } 30 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 31 Giới thiệu bài tập lớn: Ô số sudoku Ô số sudoku: mức độ dễ (1) Source: http://www.sudokukingdom.com/ 33 Ô số sudoku: mức độ dễ (2) Source: http://www.sudokukingdom.com/ 34 Ô số sudoku: khó vừa Source: http://www.sudokukingdom.com/ ...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 liệu từ bàn phím Bài tập Giới thiệu tập lớn: ô số sudoku Các lệnh lựa chọn Các lệnh lựa chọn    if if... Integer.parseInt(args[i]); } } } } Chạy chương trình: >java Test 15 19 150 >java Test 23 Nhập liệu từ bàn phím InputStreamReader InputStreamReader br = new InputStreamReader(System.in); try { String... System.out.println("invalid day of week!"); break; } } }} Chạy chương trình: >java Test thursday >java Test 10 invalid day of week! Các lệnh lặp while public class Test{ public static void main(String

Ngày đăng: 03/12/2015, 16:00

Từ khóa liên quan

Mục lục

  • Slide 1

  • Slide 2

  • Slide 3

  • Slide 4

  • Slide 5

  • Slide 6

  • Slide 7

  • Slide 8

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Slide 14

  • Slide 15

  • Slide 16

  • Slide 17

  • Slide 18

  • Slide 19

  • Slide 20

Tài liệu cùng người dùng

Tài liệu liên quan