package Inet;
import java.io.InputStreamReader;
import java.util.Scanner;
public class baisonguyen {
/**
* @param args
*/
static int countPhanTu;
static int []mang;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(new InputStreamReader(System.in));
System.out.println("Nhap so phan tu:");
countPhanTu=scan.nextInt();
mang=new int[countPhanTu];
System.out.println("Nhap vao cac gia tri cho phan tu:");
for(int i=0;i<countPhanTu;i++)
{
System.out.print("mang["+i+"]=");
mang[i]=scan.nextInt();
}
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("---------------------------------------");
System.out.println("Menu");
System.out.println("Nhap T truy suat pt");
System.out.println("Nhap N de them 1 so pt");
System.out.println("Nhap X de xoa 1 phan tu");
System.out.println("Nhap L de liet ke");
System.out.println("Nhap TK de tim kiem");
System.out.println("Nhap S de sap xep");
System.out.println("Nhap E de thoat chuong trinh");
while(true)
{
String strMenu=scan.nextLine().toUpperCase();
switch (strMenu) {
case "T":
System.out.print("Nhap vi tri pt can truy suat :");
int intSelec=scan.nextInt();
truyxuatphantu(intSelec);
System.out.print("Nhap cac phim de tiep tuc ");
break;
case "N":
System.out.print("Nhap pt can them :");
int intadd=scan.nextInt();
themmotpt(intadd);
System.out.print("Nhap cac phim de tiep tuc ");
break;
case "X":
System.out.print("Nhap vi pt ca xoa :");
int intxoa=scan.nextInt();
xoaphantuthui(intxoa);
break;
case "L":
break;
case "TK":
break;
case "S":
break;
case "E":
break;
}
}
}
public static void truyxuatphantu(int i)
{
System.out.println("phan tu thu "+i+" cua mang la "+mang[i]);
}
public static void xoaphantuthui(int i)
{
int []tmp=new int[mang.length-1];
if(i==0)
{
for (int j = 0; j < tmp.length; j++) {
tmp[j]=mang[j+1];
}
}
else if(i==mang.length-1)
{
for (int j = 0; j < tmp.length; j++) {
tmp[j]=mang[j];
}
}
else
{
for (int j = 0; j < tmp.length; j++) {
if(j<i)
{
tmp[j]=mang[j];
}
else if(j>=i)
{
tmp[j]=mang[j+1];
}
}
}
System.out.println("mang sau khi xoa phan tu thuw "+i+" la:");
for (int j = 0; j < tmp.length; j++) {
System.out.print(tmp[j]+" ");
}
}
public static void themmotpt(int a)
{
int []m=new int[mang.length+1];
for (int i = 0; i < mang.length; i++) {
m[i]=mang[i];
}
m[mang.length]=a;
System.out.println("mang sau khi them phan tu "+a+" la:");
for (int j = 0; j < m.length; j++) {
System.out.print(m[j]+" ");
}
}
}
Không có nhận xét nào:
Đăng nhận xét