運用java編寫一個簡單的電話號碼查詢系統,包含資訊的增、刪、改、查以及資訊的錄入等一些簡單的實現!
工具/原料
計算機
Java基礎
方法/步驟
錄入資訊:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Bl {
static String [][] b=new String [10][4];
static String [][]a=new String [3][4];
public static void main(String[] args)
{
System.out.println("錄入資訊:");
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
Scanner scanner=new Scanner(System.in);
if(j==0) System.out.println("姓名:");
if(j==1) System.out.println("籍貫");
if(j==2) System.out.println("電話號碼");
if(j==3) System.out.println("所在部門");
String s=scanner.next();
a[i][j]=s;
}
}
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
b[i][j]=a[i][j];
}
}
mune();
}
目錄:
static void mune(){
while(true)
{
System.out.println("1、********** 全部人物資訊;**********");
System.out.println("2、********** 刪除人物資訊;**********");
System.out.println("3、********** 修改人物資訊;**********");
System.out.println("4、********** 增加人物資訊;**********");
System.out.println("5、********** 按照姓名查詢;**********");
System.out.println("6、********** 按照部門查詢;**********");
System.out.println("7、********** 電話號碼查詢;**********");
System.out.println("8、********** 按照部門輸出;**********");
System.out.println("9、********** 程式執行結束;**********");
System.out.println(" ***********你選擇的專案:**********");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<0) System.out.println("**********請輸入1~7之間的選擇,謝謝!**********");
if(n>9) System.out.println("**********請輸入1~7之間的選擇,謝謝!**********");
switch(n)
{
case 1:
print(b); break;
case 2:
remove(b); break;
case 3:
change(b); break;
case 4:
increase(b); break;
case 5:
namefind(b); break;
case 6:
chargefind(b); break;
case 7:
numberfind(b); break;
case 8:
chargeprint(b); break;
case 9:
end(); break;
}
}
}
輸出
//輸出資訊
static void print(String[][] b)
{
System.out.println("********** 所有資訊;**********");
for(int i=0;i<10;i++){
for(int j=0;j<4;j++){
System.out.print(" "+b[i][j]);
}
System.out.println();
}
}
資訊:
//刪除資訊
static void remove(String[][] b)
{
System.out.println("**********原來的資訊為:**********");
for (int i = 0; i < b.length; i++)
{
String[] arr2 = b[i];
for (int c = 0; c < arr2.length; c++)
{
System.out.print(" "+arr2[c]);
}
System.out.println();
}
System.out.println("**********輸入數值選擇對陣列第幾項進行刪除(n行m列(0
Scanner sc1=new Scanner(System.in);
int n=sc1.nextInt();
Scanner sc11=new Scanner(System.in);
int m=sc11.nextInt();
b[n][m]="null";
print(b);
mune();
}
//修改資訊
static void change(String[][] b)
{
System.out.println("**********原來的資訊為:**********");
for (int i = 0; i < b.length; i++)
{
String[] arr2 = b[i];
for (int c = 0; c < arr2.length; c++)
{
System.out.print(" "+arr2[c]);
}
System.out.println();
}
System.out.println("**********輸入數值選擇對陣列第幾項進行修改(n行m列(0
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Scanner sc1=new Scanner(System.in);
int m=sc1.nextInt();
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
b[n][m]=s;
print(b);
mune();
}
增加資訊:
static void increase(String[][] b)
{
System.out.println("**********原來的資訊為:**********");
for (int i = 0; i < b.length; i++)
{
String[] arr2 = b[i];
for (int c = 0; c < arr2.length; c++)
{
System.out.print(" "+arr2[c]);
}
System.out.println();
}
System.out.println("**********根據輸出的所有資訊,輸入數值,選擇對陣列空白行進行新增(從0開始數,第一個一行為空的行數:):**********");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<4;i++)
{
Scanner scanner=new Scanner(System.in);
if(i==0) System.out.println("姓名:");
if(i==1) System.out.println("籍貫");
if(i==2) System.out.println("電話號碼");
if(i==3) System.out.println("所在部門");
String s=scanner.next();
b[n][i]=s;
}
print(b);
mune();
//查詢資訊(姓名 )
static void namefind(String[][] b)
{
System.out.println("**********原來的資訊為**********:");
for (int i = 0; i < b.length; i++)
{
String[] arr2 = b[i];
for (int c = 0; c < arr2.length; c++)
{
System.out.print(" "+ arr2[c]);
}
System.out.println();
}
System.out.println("資訊的有效行數(從1開始數):");
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
System.out.println("**********輸入姓名,輸出對應的資訊:**********");
String s = " ";
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
}catch(IOException e)
{
System.out.println("**********產生異常**********");
}
System.out.println();
for(int z=0;z
{
if(b[z][0].equals(s)==true)
{
for(int x=0;x<4;x++)
System.out.print(" "+b[z][x]);
}
}
System.out.println();
mune();
}
//查詢資訊(部門)
static void chargefind(String[][] b)
{
System.out.println("**********原來的資訊為:**********");
for (int i = 0; i < b.length; i++)
{
String[] arr2 = b[i];
for (int c = 0; c < arr2.length; c++)
{
System.out.print(" "+arr2[c]);
}
System.out.println();
}
System.out.println("資訊的有效行數(從1開始數):");
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
System.out.println("**********輸入所在部門,輸出對應的資訊(從0開始):**********");
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
for(int z=0;z
{
if(b[z][3].equals(s)==true)
{
for(int x=0;x<4;x++)
System.out.print(" "+b[z][x]);
}
}
System.out.println();
mune();
}
// 電話號碼查詢
static void numberfind(String[][] b){
System.out.println("**********原來的資訊為:**********");
for (int i = 0; i < b.length; i++)
{
String[] arr2 = b[i];
for (int c = 0; c < arr2.length; c++)
{
System.out.print(" "+arr2[c]);
}
System.out.println();
}
System.out.println("資訊的有效行數(從1開始數):");
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
System.out.println("**********輸入電話號碼,輸出對應的資訊(從0開始):**********");
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
for(int z=0;z
{
if(b[z][2].equals(s)==true)
{
for(int x=0;x<4;x++)
System.out.print(" "+b[z][x]);
}
}
System.out.println();
mune();
}
//按部門輸出
static void chargeprint(String[][] b)
{
String temp = null;
System.out.println("**********原來的資訊為:**********");
for (int i = 0; i < b.length; i++)
{
String[] arr2 = b[i];
for (int c = 0; c < arr2.length; c++)
{
System.out.print(" "+arr2[c]);
}
System.out.println();
}
System.out.println("資訊的有效行數(從1開始數):");
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
System.out.println("**********輸入所在部門,輸出對應的資訊(從0開始):**********");
Scanner scanner=new Scanner(System.in);
String s=scanner.next();
for(int z=0;z
{
if(b[z][3].equals(s)==true)
{
for(int j=0;j<4;j++)
System.out.print(" "+b[z][j]);
System.out.println();
}
}
for(int z=0;z
{
if(b[z][3].equals(s)!=true)
{
for(int j=0;j<4;j++)
System.out.print(" "+b[z][j]);
}
System.out.println();
}
System.out.println();
mune();
}
static void end()
{
System.out.println("程式執行結束!");
System.exit(0);
}
}
static void end()
{
System.out.println("程式執行結束!");
System.exit(0);
}
}(是與第一步驟連線的括號)