The Switch-Case Construct – cấu trúc switch – case (java)
Chào các bạn hôm trước tôi trình bày cấu trúc if – else
Nay sẽ trình bày tiếp thêo cấu trúc switch – case
The Switch-Case Construct
Cú pháp:
switch( Expression or variable name)
{
case exp1:
Statements;
break;
case exp2:
Statements;
break;
case exp3:
Statements;
break;
default:
Statements;
}
Ví dụ:
class switchcase{
public static void main(String[]args){
char x=’d';
switch(x){
case ‘a’:
System.out.println(“Gia tri cua x la :”+x);
break;
case ‘h’:
System.out.println(“Gia tri cua x la :”+x);
break;
default:
System.out.println(“Khong co gia tri nao tru hop”);
}
}
}

Ví dụ thêm về cấu trúc switch – case với số int
class switchcaseso{
public static void main(String[]args){
int vietmatrix=8;
switch(vietmatrix){
case 1:
System.out.println(“Viet Matrix duoc gan gia tri la:”+vietmatrix);
break;
case 2:
System.out.println(“Viet Matrix duoc gan gia tri la:”+vietmatrix);
break;
case 3:
System.out.println(“Viet Matrix duoc gan gia tri la:”+vietmatrix);
break;
case 8:
System.out.println(“Viet Matrix duoc gan gia tri la:”+vietmatrix);
break;
default:
System.out.println(“Viet Matrix Revolutions”);
}
}
}
Các bạn có thể download code tại đây
Ghi rõ nguồn Viet Matrrix khi phát hành lại thông tin này
Phản hồi gần đây