SlideShare a Scribd company logo
1 of 14
Download to read offline
Bài tập tuần 2 
Họ tên: Nguyễn Trung Công 
MSSV : 20121341 
Bài 1 
1.1 
public class MainActivity { 
public static void main(String[] args) { 
int a=5; 
double b=96.97; 
char c='t'; 
boolean d=true; 
System.out.println(a+"n"+b+"n"+c+"n"+d); 
} 
} 
1.2 
Chương trình này Không chạy được vì biến intNumber là kiểu nguyên còn doubleNumber kiểu double nên Không thể gán cho nhau. 
Để chương trình có thể chạy thì ta phải ép kiểu cho biến doubleNumber: 
intNumber = (int)DoubleNumber; 
1.3 
Biến character lúc đầu được gán giá trị ‘E’. Sau đó thì được gán giá trị (char)76 là ký tự ‘L’. 
1.4 
Kết quả in ra màn hình là: 
Equation 1: 13.5 
Equation 2: 56.25 
Equation 3: 4.528.5 
Equation 4: 33.0
1.5 
Tên biến 
Giá trị dự đoán 
Giải thích 
Num1 
2 
4/2=2 
Num2 
1 
Chia lấy dư 
Num3 
3 
Num1+num2 
Num4 
9 
Num3*num3 
Num5 
4.5 
2.0 => Kiểu double 
Num6 
4.0 
9/2=> Chuyển về số nguyên trước là 4, rồi chuyển sang kiểu double thành 4.0 
Bài 2 
2.1 
import java.io.*; 
public class MainActivity { 
public static void main(String[] args) { 
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
double a,b; 
try { 
a = Double.parseDouble(br.readLine()); 
b=Double.parseDouble(br.readLine()); 
System.out.println((a>=b?a:b)); 
} catch (NumberFormatException | IOException e) { 
e.printStackTrace(); 
} 
} 
}
2.2 
import java.util.Scanner; 
public class Main { public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Nhap nhiet do: "); 
try { 
int t=Integer.parseInt(sc.nextLine()); 
if (t<=15) { 
System.out.println("Giời này chỉ ở nhà chơi game là sướng."); 
}else if(t<30){ 
System.out.println("Thời tiết tuyệt vời để đi mua sắm!"); 
}else{ 
System.out.println("Các bãi biển đang chờ bạn đó!"); 
} 
} catch (Exception e) { 
System.out.println("Nhap 1 so nguyen."); 
} } 
} 
2.3 
Sử dụng if else 
import java.util.Scanner; 
public class Main { 
private static Scanner sc; 
public static void main(String[] args) { 
System.out.println("Menu"); 
System.out.println("1 - Sup bao ngu"); 
System.out.println("2 - Bo bit tet"); 
System.out.println("3 - Banh pizza"); 
sc = new Scanner(System.in); 
System.out.println("Nhap 1 so nguyen: "); 
try { 
int t=Integer.parseInt(sc.nextLine()); 
if (t==1) { 
System.out.println("Đây là một món khai vị tuyệt vời"); 
}else if(t==2){ 
System.out.println("Một trong những món ăn thông dụng ở phương Tây"); 
}else if(t==3){ 
System.out.println("Đặc sản Ý, ngon"); 
}else{ 
System.out.println("Bạn không thích món ăn nào cả sao?"); 
} 
} catch (Exception e) { 
System.out.println("Nhap 1 so nguyen.");
} 
} 
} 
Sử dụng switch 
import java.util.Scanner; 
public class Main { 
private static Scanner sc; 
public static void main(String[] args) { 
System.out.println("Menu"); 
System.out.println("1 - Sup bao ngu"); 
System.out.println("2 - Bo bit tet"); 
System.out.println("3 - Banh pizza"); 
sc = new Scanner(System.in); 
System.out.println("Nhap 1 so nguyen: "); 
try { 
int t=Integer.parseInt(sc.nextLine()); 
switch (t) { 
case 1: 
System.out.println("Đây là một món khai vị tuyệt vời"); 
break; 
case 2: 
System.out.println("Một trong những món ăn thông dụng ở phương Tây"); 
break; 
case 3: 
System.out.println("Đặc sản Ý, ngon"); 
break; 
default: 
System.out.println("Bạn không thích món ăn nào cả sao?"); 
break; 
} 
} catch (Exception e) { 
System.out.println("Nhap 1 so nguyen."); 
} 
} 
} 
2.4 
Vòng lặp sẽ chạy vo hạn gây hết bộ nhớ hệ thống. 
public class goodCode(){ 
public static void main(string[] args){ 
int age=1;
while(age<18)System.out.println(“Age ”+ age++ +” you are not old enough to vote); 
System.out.println(“Age “+ age +” you are now old enough to vote”); 
} 
} 
2.5 
While(i<21)System.out.println(i++); 
2.6 
import java.util.Scanner; 
public class Main { 
private static Scanner sc; 
public static void main(String[] args) { 
int t; 
sc=new Scanner(System.in); 
do { 
t=Integer.parseInt(sc.nextLine()); 
for (int i = 0; i < t; i++) { 
System.out.print("#"); 
} 
} while (t<0); 
} 
} 
Bài 3 
3.1 
public class Main { 
public static void main(String[] args) { 
String a="Toi", b="an", c="com"; 
String str=a+b+c; 
System.out.println(str); 
System.out.println(str.toUpperCase()); 
System.out.println(str.length()); 
} 
} 
3.2 
public class Main { 
public static void main(String[] args) { 
String str="I am line"; 
System.out.println(str+"n"+str.replace('l', 'f')); 
} 
}
3.3 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println(sc.nextLine()); 
} 
} 
3.4 
public class Main { 
public static void main(String[] args) { 
System.out.println("This is a 'message' using escape characters"); 
} 
} 
3.5 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Ten ban la gi: "); 
String t=sc.nextLine(); 
System.out.println("Xin chao "+t); 
System.out.println("Hay nhap ten cua ban:"); 
t=sc.nextLine(); 
String[] split=t.split(" "); 
for (int i = 0; i < split.length; i++) { 
if(i==0)System.out.print(split[i]+", "); 
System.out.print(split[i].substring(0, 1)+"."); 
} 
} 
} 
3.6 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Nhap khoi luong vat: "); 
float a=Float.parseFloat(sc.nextLine()); 
System.out.println("Nhap van toc vat"); 
float b=Float.parseFloat(sc.nextLine()); 
System.out.println("Dong nang la: "+(a*b*b/2)); 
} 
}
3.7 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Ban co thich dau hay khong: "); 
String a=sc.nextLine(); 
if (a.equalsIgnoreCase("co")) { 
System.out.println("Moi chuong trinh Java deu giong dau"); 
}else if(a.equalsIgnoreCase("khong")){ 
System.out.println("Dung lo, khi lon len ban se thich chung"); 
}else{ 
System.out.println("Chua quyet dinh sao."); 
} 
} 
}
3.8 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Nhap x:"); float x=Float.parseFloat(sc.nextLine()); 
float eMuX = 0, cosX=0, sinX=0; 
int n=10; 
for (int i = 0; i < n; i++) { 
eMuX+=Math.pow(x, i)/giaithua(i); 
cosX+=Math.pow(-1, i)*Math.pow(x, 2*i)/giaithua(2*i); 
sinX+=Math.pow(-1, i)*Math.pow(x, 2*i+1)/giaithua(2*i+1); 
} 
System.out.println(eMuX + " : "+Math.exp(x) ); 
System.out.println(cosX+" : "+Math.cos(x)); 
System.out.println(sinX+" : "+ Math.sin(x)); 
} 
public static int giaithua(int a){ 
int b=1; 
for (int i = 1; i < a+1; i++) { 
b*=i; 
} 
return b; 
} 
} 
3.9 
import java.util.Map; 
import java.util.Scanner; 
import java.util.TreeMap; 
public class Main { 
static Map<String, Integer> map; 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Nhap 1 dong van ban: "); 
String str=sc.nextLine(); 
str=str.replaceAll(" ", "").toUpperCase(); 
map=new TreeMap<String, Integer>(); 
for (int i = 0; i < str.length(); i++) { 
String g=find(str.substring(i, i+1)); 
if (g!=null) { 
map.put(g, 1); 
} 
} 
for (Map.Entry entry : map.entrySet()) { 
System.out.print(entry.getKey()+" : "); 
for (int i = 0; i < (int)entry.getValue(); i++) { 
System.out.print("*"); 
} 
System.out.println(); 
}
} 
public static String find(String c){ 
for (Map.Entry entry : map.entrySet()) { 
if (c.equals((String)entry.getKey())) { 
entry.setValue((int)entry.getValue()+1); 
return null; 
} 
} 
return c; 
} 
} 
3.10 
Sử dụng vòng lặp while. 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Kich thuoc hop la bao nhieu? "); 
int x=Integer.parseInt(sc.nextLine()); 
int i=0; 
while (i<x) { 
if (i==0 | i==x-1) { 
for (int j = 0; j < x; j++) { 
System.out.print("* "); 
} 
}else{ 
System.out.print("* "); 
for (int j = 0; j < x-2; j++) { 
System.out.print(" "); 
} 
System.out.print("*"); 
} 
System.out.println(); 
i++; 
} 
} 
} 
Sử dụng vòng lặp For 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Kich thuoc hop la bao nhieu? "); 
int x=Integer.parseInt(sc.nextLine()); 
for (int i = 0; i < x; i++) { 
if (i==0 | i==x-1) { 
for (int j = 0; j < x; j++) { 
System.out.print("* "); 
} 
}else{
System.out.print("* "); 
for (int j = 0; j < x-2; j++) { 
System.out.print(" "); 
} 
System.out.print("*"); 
} 
System.out.println(); 
} 
} 
} 
Sử dụng vòng lặp do..while 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Kich thuoc hop la bao nhieu? "); 
int x=Integer.parseInt(sc.nextLine()); 
int i=0; 
do { 
if (i==0 | i==x-1) { 
for (int j = 0; j < x; j++) { 
System.out.print("* "); 
} 
}else{ 
System.out.print("* "); 
for (int j = 0; j < x-2; j++) { 
System.out.print(" "); 
} 
System.out.print("*"); 
} 
System.out.println(); 
i++; 
} while (i<x); 
} 
} 
3.11 
public class Main { 
public static void main(String[] args) { 
for (int i = 1; i < 13; i++) { 
for (int j = 1; j < 13; j++) { 
System.out.print(i*j+"t"); 
} 
System.out.println(); 
} 
} 
}
3.12 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Kich thuoc hop la bao nhieu? "); 
int x=Integer.parseInt(sc.nextLine()); 
for (int i = 0; i <= x/2; i++) { 
for (int j = i; j < x/2; j++) { 
System.out.print(" "); 
} 
for (int j = 1; j < i*2+1; j++) { 
System.out.print("*"); 
} 
System.out.print("*n"); 
} 
for (int i = x/2; i > 0; i--) { 
for (int j = i; j <=x/2; j++) { 
System.out.print(" "); 
} 
for (int j = 1; j < i*2-1; j++) { 
System.out.print("*"); 
} 
System.out.print("*n"); 
} 
} 
} 
Bài 4 
4.1 
Không sử dụng mảng 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Nhap vao 5 so nguyen: "); 
String str=sc.nextLine(); 
str=str.trim().replaceAll("s+", " "); 
for (int i = 0; i < str.length(); i+=2) { 
for (int j = 0; j < Integer.parseInt(str.substring(i, i+1)); j++) { 
System.out.print("*"); 
} 
System.out.println(); 
} 
} 
}
Sử dụng mảng: 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Nhap vao 5 so nguyen: "); 
String str=sc.nextLine(); 
str=str.trim().replaceAll("s+", " "); 
String[] x=str.split(" "); 
for (String string : x) { 
for (int i = 0; i < Integer.parseInt(string); i++) { 
System.out.print("*"); 
} 
System.out.println(); 
} 
} 
} 
Hiển thị theo chiều dọc 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Nhap vao 5 so nguyen: "); 
String str=sc.nextLine(); 
str=str.trim().replaceAll("s+", " "); 
String[] array=str.split(" "); 
int max=0; 
for (String string : array) { 
if(Integer.parseInt(string)>max)max=Integer.parseInt(string); 
} 
String[][] strArray=new String[max][5]; 
for (int i = 0; i < max; i++) { 
for (int j = 0; j < 5; j++) { 
strArray[i][j]=" "; 
} 
} 
int k=0; 
for (String string : array) { 
for (int i = max-1; i > max-Integer.parseInt(string)-1; i- -) { strArray[i][k]="*"; 
} 
k++; 
} 
for (int i = 0; i < max; i++) { 
for (int j = 0; j < 5; j++) { 
System.out.print(strArray[i][j]); 
} 
System.out.println(); 
} 
}
} 
Chương trình đã có thể chạy với số lượng Không hạn chế. 
4.2 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Nhap vao 5 so nguyen: "); 
String str=sc.nextLine(); 
str=str.trim().replaceAll("s+", " "); 
String[] array=str.split(" "); 
System.out.println(str); 
int sum=0; 
for (String string : array) { 
sum+=Integer.parseInt(string); 
} 
int tb=sum/array.length; System.out.println("TB cong cua cac so do la: "+tb); 
int nho=0; 
for (String string : array) { if(Integer.parseInt(string)<tb)nho++; 
} 
System.out.println("So cac so nho hon trung binh: "+nho); 
System.out.println("So cac so lon hon trung binh: "+(array.length-nho)); 
} 
} 
4.3 
import java.util.Map; 
import java.util.Scanner; 
import java.util.TreeMap; 
public class Main { 
static Map<String, Integer> map; 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Nhap vao 5 so nguyen: "); 
String str=sc.nextLine(); 
str=str.trim().replaceAll(" ", "").toUpperCase(); 
map=new TreeMap<String, Integer>(); 
for (int i = 0; i < str.length(); i++) { 
String g=find(str.substring(i, i+1)); 
if (g!=null) { 
map.put(g, 1); 
} 
} 
for (Map.Entry entry : map.entrySet()) { 
System.out.println(entry.getKey()+" : "+entry.getValue()); 
} 
} 
public static String find(String c){ 
for (Map.Entry entry : map.entrySet()) { 
if (c.equals((String)entry.getKey())) {
entry.setValue((int)entry.getValue()+1); 
return null; 
} 
} 
return c; 
} 
} 
4.4 
import java.util.Arrays; 
import java.util.Scanner; 
public class Main { 
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
System.out.println("Nhap vao 5 so nguyen: "); 
String str=sc.nextLine(); 
str=str.trim().replaceAll("s+", " "); 
String[] array=str.split(" "); 
int[] intArray=new int[array.length]; 
for (int i = 0; i < array.length; i++) { 
intArray[i]=Integer.parseInt(array[i]); 
} 
println(intArray); 
Arrays.sort(intArray); 
System.out.println(); 
println(intArray); 
} 
private static void println(int[] intArray) { for (int i = 0; i < intArray.length; i++) { System.out.print(intArray[i]+" "); 
} 
} 
} 
4.5 
Kết quả là: 
01234 
00000 
Kết quả khác nhau là vì khi Point B dc chạy thì trc đó mảng đã đưa về 0 hết rồi.

More Related Content

What's hot

Hace una calculadora en jeank
Hace una calculadora en jeankHace una calculadora en jeank
Hace una calculadora en jeankHumbertoWuwu
 
RxJava, Getting Started - David Wursteisen - 16 Octobre 2014
RxJava, Getting Started - David Wursteisen - 16 Octobre 2014RxJava, Getting Started - David Wursteisen - 16 Octobre 2014
RxJava, Getting Started - David Wursteisen - 16 Octobre 2014SOAT
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Loïc Knuchel
 
Pogram menghitung bangun datar dan ruang dengan java
Pogram menghitung bangun datar dan ruang dengan javaPogram menghitung bangun datar dan ruang dengan java
Pogram menghitung bangun datar dan ruang dengan javaLendra Susanto
 
Baocao ltjava
Baocao ltjavaBaocao ltjava
Baocao ltjavaTnt Ttđ
 
Project Komputer Grafik
Project Komputer GrafikProject Komputer Grafik
Project Komputer GrafikHamimSuyuti
 
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOSEJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOSDarwin Durand
 
RIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JSRIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JSDominik Jungowski
 
Шаблоны проектирования 2
Шаблоны проектирования 2Шаблоны проектирования 2
Шаблоны проектирования 2Constantin Kichinsky
 

What's hot (20)

Hace una calculadora en jeank
Hace una calculadora en jeankHace una calculadora en jeank
Hace una calculadora en jeank
 
Java
JavaJava
Java
 
1- Sourcecode Array
1- Sourcecode Array1- Sourcecode Array
1- Sourcecode Array
 
RxJava, Getting Started - David Wursteisen - 16 Octobre 2014
RxJava, Getting Started - David Wursteisen - 16 Octobre 2014RxJava, Getting Started - David Wursteisen - 16 Octobre 2014
RxJava, Getting Started - David Wursteisen - 16 Octobre 2014
 
Tugas praktek modul isbd
Tugas praktek modul isbdTugas praktek modul isbd
Tugas praktek modul isbd
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
 
Pogram menghitung bangun datar dan ruang dengan java
Pogram menghitung bangun datar dan ruang dengan javaPogram menghitung bangun datar dan ruang dengan java
Pogram menghitung bangun datar dan ruang dengan java
 
Baocao ltjava
Baocao ltjavaBaocao ltjava
Baocao ltjava
 
Linked list proj
Linked list projLinked list proj
Linked list proj
 
Project Komputer Grafik
Project Komputer GrafikProject Komputer Grafik
Project Komputer Grafik
 
Dokumen
DokumenDokumen
Dokumen
 
Makalah game sudoku
Makalah game sudokuMakalah game sudoku
Makalah game sudoku
 
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOSEJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
 
Trabajo de programacion
Trabajo de programacionTrabajo de programacion
Trabajo de programacion
 
20150415 csharp6.0
20150415 csharp6.020150415 csharp6.0
20150415 csharp6.0
 
RIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JSRIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JS
 
デバッグ戦略
デバッグ戦略デバッグ戦略
デバッグ戦略
 
Шаблоны проектирования 2
Шаблоны проектирования 2Шаблоны проектирования 2
Шаблоны проектирования 2
 
Package pack parnon
Package pack parnonPackage pack parnon
Package pack parnon
 
Taller de string(java)
Taller de string(java)Taller de string(java)
Taller de string(java)
 

Bài tập tuần 2

  • 1. Bài tập tuần 2 Họ tên: Nguyễn Trung Công MSSV : 20121341 Bài 1 1.1 public class MainActivity { public static void main(String[] args) { int a=5; double b=96.97; char c='t'; boolean d=true; System.out.println(a+"n"+b+"n"+c+"n"+d); } } 1.2 Chương trình này Không chạy được vì biến intNumber là kiểu nguyên còn doubleNumber kiểu double nên Không thể gán cho nhau. Để chương trình có thể chạy thì ta phải ép kiểu cho biến doubleNumber: intNumber = (int)DoubleNumber; 1.3 Biến character lúc đầu được gán giá trị ‘E’. Sau đó thì được gán giá trị (char)76 là ký tự ‘L’. 1.4 Kết quả in ra màn hình là: Equation 1: 13.5 Equation 2: 56.25 Equation 3: 4.528.5 Equation 4: 33.0
  • 2. 1.5 Tên biến Giá trị dự đoán Giải thích Num1 2 4/2=2 Num2 1 Chia lấy dư Num3 3 Num1+num2 Num4 9 Num3*num3 Num5 4.5 2.0 => Kiểu double Num6 4.0 9/2=> Chuyển về số nguyên trước là 4, rồi chuyển sang kiểu double thành 4.0 Bài 2 2.1 import java.io.*; public class MainActivity { public static void main(String[] args) { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); double a,b; try { a = Double.parseDouble(br.readLine()); b=Double.parseDouble(br.readLine()); System.out.println((a>=b?a:b)); } catch (NumberFormatException | IOException e) { e.printStackTrace(); } } }
  • 3. 2.2 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Nhap nhiet do: "); try { int t=Integer.parseInt(sc.nextLine()); if (t<=15) { System.out.println("Giời này chỉ ở nhà chơi game là sướng."); }else if(t<30){ System.out.println("Thời tiết tuyệt vời để đi mua sắm!"); }else{ System.out.println("Các bãi biển đang chờ bạn đó!"); } } catch (Exception e) { System.out.println("Nhap 1 so nguyen."); } } } 2.3 Sử dụng if else import java.util.Scanner; public class Main { private static Scanner sc; public static void main(String[] args) { System.out.println("Menu"); System.out.println("1 - Sup bao ngu"); System.out.println("2 - Bo bit tet"); System.out.println("3 - Banh pizza"); sc = new Scanner(System.in); System.out.println("Nhap 1 so nguyen: "); try { int t=Integer.parseInt(sc.nextLine()); if (t==1) { System.out.println("Đây là một món khai vị tuyệt vời"); }else if(t==2){ System.out.println("Một trong những món ăn thông dụng ở phương Tây"); }else if(t==3){ System.out.println("Đặc sản Ý, ngon"); }else{ System.out.println("Bạn không thích món ăn nào cả sao?"); } } catch (Exception e) { System.out.println("Nhap 1 so nguyen.");
  • 4. } } } Sử dụng switch import java.util.Scanner; public class Main { private static Scanner sc; public static void main(String[] args) { System.out.println("Menu"); System.out.println("1 - Sup bao ngu"); System.out.println("2 - Bo bit tet"); System.out.println("3 - Banh pizza"); sc = new Scanner(System.in); System.out.println("Nhap 1 so nguyen: "); try { int t=Integer.parseInt(sc.nextLine()); switch (t) { case 1: System.out.println("Đây là một món khai vị tuyệt vời"); break; case 2: System.out.println("Một trong những món ăn thông dụng ở phương Tây"); break; case 3: System.out.println("Đặc sản Ý, ngon"); break; default: System.out.println("Bạn không thích món ăn nào cả sao?"); break; } } catch (Exception e) { System.out.println("Nhap 1 so nguyen."); } } } 2.4 Vòng lặp sẽ chạy vo hạn gây hết bộ nhớ hệ thống. public class goodCode(){ public static void main(string[] args){ int age=1;
  • 5. while(age<18)System.out.println(“Age ”+ age++ +” you are not old enough to vote); System.out.println(“Age “+ age +” you are now old enough to vote”); } } 2.5 While(i<21)System.out.println(i++); 2.6 import java.util.Scanner; public class Main { private static Scanner sc; public static void main(String[] args) { int t; sc=new Scanner(System.in); do { t=Integer.parseInt(sc.nextLine()); for (int i = 0; i < t; i++) { System.out.print("#"); } } while (t<0); } } Bài 3 3.1 public class Main { public static void main(String[] args) { String a="Toi", b="an", c="com"; String str=a+b+c; System.out.println(str); System.out.println(str.toUpperCase()); System.out.println(str.length()); } } 3.2 public class Main { public static void main(String[] args) { String str="I am line"; System.out.println(str+"n"+str.replace('l', 'f')); } }
  • 6. 3.3 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println(sc.nextLine()); } } 3.4 public class Main { public static void main(String[] args) { System.out.println("This is a 'message' using escape characters"); } } 3.5 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Ten ban la gi: "); String t=sc.nextLine(); System.out.println("Xin chao "+t); System.out.println("Hay nhap ten cua ban:"); t=sc.nextLine(); String[] split=t.split(" "); for (int i = 0; i < split.length; i++) { if(i==0)System.out.print(split[i]+", "); System.out.print(split[i].substring(0, 1)+"."); } } } 3.6 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Nhap khoi luong vat: "); float a=Float.parseFloat(sc.nextLine()); System.out.println("Nhap van toc vat"); float b=Float.parseFloat(sc.nextLine()); System.out.println("Dong nang la: "+(a*b*b/2)); } }
  • 7. 3.7 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Ban co thich dau hay khong: "); String a=sc.nextLine(); if (a.equalsIgnoreCase("co")) { System.out.println("Moi chuong trinh Java deu giong dau"); }else if(a.equalsIgnoreCase("khong")){ System.out.println("Dung lo, khi lon len ban se thich chung"); }else{ System.out.println("Chua quyet dinh sao."); } } }
  • 8. 3.8 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Nhap x:"); float x=Float.parseFloat(sc.nextLine()); float eMuX = 0, cosX=0, sinX=0; int n=10; for (int i = 0; i < n; i++) { eMuX+=Math.pow(x, i)/giaithua(i); cosX+=Math.pow(-1, i)*Math.pow(x, 2*i)/giaithua(2*i); sinX+=Math.pow(-1, i)*Math.pow(x, 2*i+1)/giaithua(2*i+1); } System.out.println(eMuX + " : "+Math.exp(x) ); System.out.println(cosX+" : "+Math.cos(x)); System.out.println(sinX+" : "+ Math.sin(x)); } public static int giaithua(int a){ int b=1; for (int i = 1; i < a+1; i++) { b*=i; } return b; } } 3.9 import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class Main { static Map<String, Integer> map; public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Nhap 1 dong van ban: "); String str=sc.nextLine(); str=str.replaceAll(" ", "").toUpperCase(); map=new TreeMap<String, Integer>(); for (int i = 0; i < str.length(); i++) { String g=find(str.substring(i, i+1)); if (g!=null) { map.put(g, 1); } } for (Map.Entry entry : map.entrySet()) { System.out.print(entry.getKey()+" : "); for (int i = 0; i < (int)entry.getValue(); i++) { System.out.print("*"); } System.out.println(); }
  • 9. } public static String find(String c){ for (Map.Entry entry : map.entrySet()) { if (c.equals((String)entry.getKey())) { entry.setValue((int)entry.getValue()+1); return null; } } return c; } } 3.10 Sử dụng vòng lặp while. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Kich thuoc hop la bao nhieu? "); int x=Integer.parseInt(sc.nextLine()); int i=0; while (i<x) { if (i==0 | i==x-1) { for (int j = 0; j < x; j++) { System.out.print("* "); } }else{ System.out.print("* "); for (int j = 0; j < x-2; j++) { System.out.print(" "); } System.out.print("*"); } System.out.println(); i++; } } } Sử dụng vòng lặp For import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Kich thuoc hop la bao nhieu? "); int x=Integer.parseInt(sc.nextLine()); for (int i = 0; i < x; i++) { if (i==0 | i==x-1) { for (int j = 0; j < x; j++) { System.out.print("* "); } }else{
  • 10. System.out.print("* "); for (int j = 0; j < x-2; j++) { System.out.print(" "); } System.out.print("*"); } System.out.println(); } } } Sử dụng vòng lặp do..while import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Kich thuoc hop la bao nhieu? "); int x=Integer.parseInt(sc.nextLine()); int i=0; do { if (i==0 | i==x-1) { for (int j = 0; j < x; j++) { System.out.print("* "); } }else{ System.out.print("* "); for (int j = 0; j < x-2; j++) { System.out.print(" "); } System.out.print("*"); } System.out.println(); i++; } while (i<x); } } 3.11 public class Main { public static void main(String[] args) { for (int i = 1; i < 13; i++) { for (int j = 1; j < 13; j++) { System.out.print(i*j+"t"); } System.out.println(); } } }
  • 11. 3.12 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Kich thuoc hop la bao nhieu? "); int x=Integer.parseInt(sc.nextLine()); for (int i = 0; i <= x/2; i++) { for (int j = i; j < x/2; j++) { System.out.print(" "); } for (int j = 1; j < i*2+1; j++) { System.out.print("*"); } System.out.print("*n"); } for (int i = x/2; i > 0; i--) { for (int j = i; j <=x/2; j++) { System.out.print(" "); } for (int j = 1; j < i*2-1; j++) { System.out.print("*"); } System.out.print("*n"); } } } Bài 4 4.1 Không sử dụng mảng import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Nhap vao 5 so nguyen: "); String str=sc.nextLine(); str=str.trim().replaceAll("s+", " "); for (int i = 0; i < str.length(); i+=2) { for (int j = 0; j < Integer.parseInt(str.substring(i, i+1)); j++) { System.out.print("*"); } System.out.println(); } } }
  • 12. Sử dụng mảng: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Nhap vao 5 so nguyen: "); String str=sc.nextLine(); str=str.trim().replaceAll("s+", " "); String[] x=str.split(" "); for (String string : x) { for (int i = 0; i < Integer.parseInt(string); i++) { System.out.print("*"); } System.out.println(); } } } Hiển thị theo chiều dọc import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Nhap vao 5 so nguyen: "); String str=sc.nextLine(); str=str.trim().replaceAll("s+", " "); String[] array=str.split(" "); int max=0; for (String string : array) { if(Integer.parseInt(string)>max)max=Integer.parseInt(string); } String[][] strArray=new String[max][5]; for (int i = 0; i < max; i++) { for (int j = 0; j < 5; j++) { strArray[i][j]=" "; } } int k=0; for (String string : array) { for (int i = max-1; i > max-Integer.parseInt(string)-1; i- -) { strArray[i][k]="*"; } k++; } for (int i = 0; i < max; i++) { for (int j = 0; j < 5; j++) { System.out.print(strArray[i][j]); } System.out.println(); } }
  • 13. } Chương trình đã có thể chạy với số lượng Không hạn chế. 4.2 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Nhap vao 5 so nguyen: "); String str=sc.nextLine(); str=str.trim().replaceAll("s+", " "); String[] array=str.split(" "); System.out.println(str); int sum=0; for (String string : array) { sum+=Integer.parseInt(string); } int tb=sum/array.length; System.out.println("TB cong cua cac so do la: "+tb); int nho=0; for (String string : array) { if(Integer.parseInt(string)<tb)nho++; } System.out.println("So cac so nho hon trung binh: "+nho); System.out.println("So cac so lon hon trung binh: "+(array.length-nho)); } } 4.3 import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class Main { static Map<String, Integer> map; public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Nhap vao 5 so nguyen: "); String str=sc.nextLine(); str=str.trim().replaceAll(" ", "").toUpperCase(); map=new TreeMap<String, Integer>(); for (int i = 0; i < str.length(); i++) { String g=find(str.substring(i, i+1)); if (g!=null) { map.put(g, 1); } } for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey()+" : "+entry.getValue()); } } public static String find(String c){ for (Map.Entry entry : map.entrySet()) { if (c.equals((String)entry.getKey())) {
  • 14. entry.setValue((int)entry.getValue()+1); return null; } } return c; } } 4.4 import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Nhap vao 5 so nguyen: "); String str=sc.nextLine(); str=str.trim().replaceAll("s+", " "); String[] array=str.split(" "); int[] intArray=new int[array.length]; for (int i = 0; i < array.length; i++) { intArray[i]=Integer.parseInt(array[i]); } println(intArray); Arrays.sort(intArray); System.out.println(); println(intArray); } private static void println(int[] intArray) { for (int i = 0; i < intArray.length; i++) { System.out.print(intArray[i]+" "); } } } 4.5 Kết quả là: 01234 00000 Kết quả khác nhau là vì khi Point B dc chạy thì trc đó mảng đã đưa về 0 hết rồi.