国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Hashtable簡易數據庫程序

2019-11-18 15:15:20
字體:
來源:轉載
供稿:網友

import java.io.*; 
import java.util.*; 
class str14 

InputStreamReader stdin=new InputStreamReader(System.in); 
BufferedReader bufin=new BufferedReader(stdin); 

public static void main(String args[]) throws IOException 

new str14();//run the application 


public str14() throws IOException//constrUCtor 

Hashtable htable=new Hashtable(20,0.75F); 
dataBase(htable); 


public str14(Hashtable htable) throws IOException //override the constructor 

dataBase(htable); 


public void dataBase(Hashtable htable) throws IOException 

int count=htable.size();//get the ammount of the data in htable 
int value,id,num; 
String key,enter,data; 
Enumeration keys=htable.keys();//get all the key in htable 
Enumeration elements=htable.elements();//get all the elements in htable 
System.out.Hashtable 簡易數據庫程序
"); 
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.print("請輸入您的選擇:"); 
value=select();//call select(),return the function number 
switch(value)//the function 

case 1: 
System.out.print("
請輸入一筆數據:");//need data input 
data=bufin.readLine(); 
count++; 
key=String.valueOf(count); 
htable.put(key,data);//store it to the htable 
System.out.print("
輸入完成,按任意鍵繼續..."); 
enter=bufin.readLine(); 
new str14(htable);//reenter 
break; 
case 2://clear all data from the htable 
htable.clear(); 
System.out.print("
已刪除了所有數據... 按任意鍵繼續..."); 
enter=bufin.readLine(); 
new str14(htable);//reenter 
break; 
case 3: 
System.out.print("
請輸入要顯示的數據編號:"); 
id=getid(count);//call getid() 
key=String.valueOf(id); 
Object select=htable.get(key);//fetch the data from the htable 
data=select.toString(); 
System.out.print("
編號"+" "+"內容"); 
System.out.println("
 "+key+" "+data);//display the data 
System.out.print("
按任意鍵繼續..."); 
enter=bufin.readLine(); 
new str14(htable);//reenter 
break; 
case 4: 
System.out.print("請輸入要刪除的數據編號:"); 
id=getid(count); 
key=String.valueOf(id); 
htable.remove(key);//remove data 
count--; 
num=count; 
System.out.print("已刪除了所選擇的數據...按任意見繼續..."); 
Hashtable htable1=new Hashtable(20,0.75F);//create new htable named htable1 
elements=htable.elements();//fetch all the data from htable 
while(elements.hasMoreElements()) 

key=String.valueOf(num);//a new value 
data=(String)elements.nextElement();//fetch content of the data 
htable1.put(key,data);//store it to htable1 
num--; 

htable.clear(); 
enter=bufin.readLine(); 
new str14(htable1); 
break; 
case 5: 
String[] sortkey=new String[count];//create a new sort array 
String[] sortdata=new String[count]; 
num=count; 
elements=htable.elements(); 
keys=htable.keys(); 
while(elements.hasMoreElements()) 

key=(String)keys.nextElement();//fetch value of key 
data=(String)elements.nextElement();//fetch value of data 
sortkey[num-1]=key;//store the value of key to sortkey array 
sortdata[num-1]=data;//store the value of data to sortdata array 
num--; 

System.out.println("
編號"+" "+"內容"); 
for(int i=0;i<count;i++)//display all content after sorting 
System.out.println(" "+sortkey[i]+" "+sortdata[i]); 
System.out.print("
目前共有"+count+"筆數據"); 
System.out.print("

按任意鍵繼續..."); 
enter=bufin.readLine(); 
new str14(htable); 
break; 
default: 



public int select() throws IOException//method of getting a function selector 

String input; 
int value=0; 
input=bufin.readLine();//read a input from keyboard 
try 

value=Integer.parseInt(input);//convert a string to a int value 
}catch(NumberFormatException e)//can’t vonverted 

System.out.print("請輸入選項1~6:"); 
value=select(); 

if(value>6value<1)//if exceed then print a message and reenter 

System.out.print("請輸入選項1~6:"); 
value=select(); 

return value;//return a value 


public int getid(int count)throws IOException//a method of return the number of data 

String input; 
int value=0; 
input=bufin.readLine();//read a user input string from keyboard 
try 

value=Integer.parseInt(input);//convert the string to a int 
}catch(NumberFormatException e)//if can’t convert to a integer then reenter 

System.out.print("請輸入數據編號:"); 
value=getid(count); 

if(value>count)//the input value is out of bound 

System.out.print("無此編號的數據,請重新輸入:"); 
getid(count); 

return value;//return a value 



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永靖县| 台中县| 江山市| 普格县| 宜宾县| 凌云县| 万年县| 海盐县| 高台县| 子洲县| 兖州市| 潜江市| 广元市| 石城县| 仁布县| 武宣县| 四川省| 通城县| 博野县| 蕉岭县| 襄垣县| 尼木县| 湛江市| 梧州市| 紫阳县| 伊川县| 清新县| 江华| 二连浩特市| 乌兰县| 磐安县| 溧水县| 江津市| 沅陵县| 香格里拉县| 江城| 奉贤区| 北碚区| 宝鸡市| 杨浦区| 什邡市|