一個關於狗記錄的Java練習

範銘祥發表於2015-03-26

package 狗場;
import java.util.*;
public class dogRoom {

/**
* 作者.範銘祥
* 狗場的狗體重查詢問題
*/

public static void main(String[] args)
{
float allweight=0;
dog xx[]=new dog[4];
for(int i=0;i<4;i++)
{
xx[i]=new dog();
}
xx[0].name="花花";
xx[0].weight=4.5f;
xx[1].name="白白";
xx[1].weight=5.6f;
xx[2].name="黑黑";
xx[2].weight=7.8f;
xx[3].name="紅紅";
xx[3].weight=9.0f;
for(int i=0;i<4;i++)
{
System.out.println(xx[i].name+xx[i].weight+"\n");
allweight=allweight+xx[i].weight;
}
System.out.println("四隻狗的總體重為"+allweight);
System.out.println("四隻狗的平均體重為"+allweight/4);
//查詢狗的系統
boolean Y=true;
Scanner in=new Scanner(System.in);

do
{
System.out.println("請輸入你要查詢的狗名:");
String name=in.next();
chaxun no1=new chaxun();
no1.chaxun1(name,xx);

}while(Y);
}

}
class dog
{
public String name;
public float weight;
}
class chaxun
{
String cc;
public void chaxun1(String name, dog[] xx) {
// TODO Auto-generated method stub
this.cc=name;
if(cc.equals("花花"))
{
System.out.println(xx[0].weight+"\n");
}
if(cc.equals("白白"))
{
System.out.println(xx[1].weight+"\n");
}
if(cc.equals("黑黑"))
{
System.out.println(xx[2].weight+"\n");
}
if(cc.equals("紅紅"))
{
System.out.println(xx[3].weight+"\n");
}

}


}

相關文章