先聲明仍然有BUG,但因時間真的有點晚,怕有同學想先交作業了就先放上來,如果有改完我會再更新此版。
**我真的太想上線殺LM懶了,Key資料測試到一個手指抽筋這禮拜作業只好放給他爛了沒意外的話不會更新囉!12:08更新
BUG1~ Case3的查詢 無法查到最後一筆資料..ex.三筆資料 只能查到1跟2..第三筆無法查詢
BUG2~ Case4的修改 無法修改第一筆資料..ex.三筆資料 只能修改2跟3..第一筆無法修改(感謝岳聰幫忙測試DEBUG Facebook 搜尋 林岳聰 就可以找到他囉! 揪咪)
BUG3~ Case5的刪除 當只有一筆資料時 無法刪除(我真的很懶的DE這個BUG 只Key一筆根本欠殺)
怕很多人沒有原始未補上Case的檔案 直接就一併放了!!
import java.util.*;
class BMI
{
String id,name;
double weight,tall,bmi;
BMI link;
BMI()
{
link=null;
}
BMI(String id,String name,double weight,double tall)
{
this();
this.id=id;
this.name=name;
this.weight=weight;
this.tall=tall;
}
void compBMI()
{
bmi=weight/(tall*tall);
}
void show()
{
System.out.printf("%10s %20s %8.4f %5.3f %8.4f\n",id,name,weight,tall,bmi);
}
}
public class Student
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int cmd;
BMI top=new BMI();
BMI p1,p2;
do
{
System.out.printf("1. 新增資料\n");
System.out.printf("2. 列印資料\n");
System.out.printf("3. 查詢資料\n");
System.out.printf("4. 修改資料\n");
System.out.printf("5. 刪除資料\n");
System.out.printf("6. 離開系統\n");
System.out.printf("==========\n");
System.out.printf("請輸入指令: \n");
cmd=sc.nextInt();
switch(cmd)
{
case 1:
System.out.printf("請輸入學號: ");
String id=sc.next();
System.out.printf("請輸入姓名: ");
String name=sc.next();
System.out.printf("請輸入體重: ");
double weight=sc.nextDouble();
System.out.printf("請輸入身高: ");
double tall=sc.nextDouble();
p1=new BMI(id,name,weight,tall);
p1.compBMI();
p1.link=top.link;
top.link=p1;
break;
case 2:
p1=top.link;
while(p1!=null)
{
p1.show();
p1=p1.link;
}
break;
case 3:
System.out.printf("請輸入要查詢的類別(1.學號2.姓名):");
int s = sc.nextInt();
switch(s)
{
case 1:
System.out.printf("請輸入欲查詢的學號(格式務必正確) :");
String sid = sc.next();
p1 = top.link;
while( p1 != null )
{
if ( p1.id.compareToIgnoreCase(sid) == 1 )
{
System.out.printf("找到%10s的資料\n",sid);
p1.link.show();
}
p1 = p1.link;
}
break;
case 2:
System.out.printf("請輸入欲查詢的姓名(格式務必正確) :");
String sname = sc.next();
p1 = top.link;
while( p1 != null )
{
if ( p1.name.compareToIgnoreCase(sname) == 1 )
{
System.out.printf("找到%10s的資料\n",sname);
p1.link.show();
}
p1 = p1.link;
}
break;
default:
System.out.printf("錯誤指令\n");
}
break;
case 4:
System.out.printf("請輸入欲修改的學生學號 :");
String rid = sc.next();
p1 = top.link;
while( p1 != null )
{
if ( p1.id.compareToIgnoreCase(rid) == 1 )
{
System.out.printf("請輸入學號: ");
p1.id=sc.next();
System.out.printf("請輸入姓名: ");
p1.name=sc.next();
System.out.printf("請輸入體重: ");
p1.weight=sc.nextDouble();
System.out.printf("請輸入身高: ");
p1.tall=sc.nextDouble();
p1.compBMI();
}
p1 = p1.link;
}
break;
case 5:
System.out.printf("請輸入欲刪除的學生學號 :");
String did = sc.next();
p1 = top.link;
p2 = p1.link;
while ( p2 != null )
{
if ( p1.id.compareToIgnoreCase(did) == 1 )
{
p1.link = p2.link;
}
else
{
p1 = p2;
p2 = p2.link;
}
}
break;
case 6:
break;
default:
System.out.printf("錯誤指令\n");
}
}while(cmd!=6);
}
}