初學JAVA一星期編的小程序(找小貓)

//做者:楠哥
//功能:找小貓
//日期:2012/3/7


import java.io.*;
public class Zhaomao 
{
public static void main(String[] args) 
{try{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
        
System.out.println("請輸入要存儲貓的個數");
String geshu = br.readLine();
int maoge = Integer.parseInt(geshu);

mao[] mao1 = new mao[maoge];

for(int i=0; i<maoge; i++){
System.out.println("請輸入貓的名字");
String maoname = br.readLine();

System.out.println("請輸入貓的年齡");
String mage = br.readLine();
int maoage = Integer.parseInt(mage);

System.out.println("請輸入貓的顏色");
String maose = br.readLine();

mao1[i] = new mao(maoname,maoage,maose);
}

        System.out.println("請輸入要找的貓的名字");
String zhaoname = br.readLine();

for(int i=0; i<maoge; i++)
if(mao1[i].findName(zhaoname) == true){
System.out.println(mao1[i]);
}}catch(Exception e)
   {
  e.printStackTrace();
  }
}
}

class mao
{
private String name;
private int age;
private String color;

//初始化小貓
public mao(String name,int age,String color){
this.name = name;
this.age = age;
this.color = color;
}

//此方法用於找貓
public boolean findName(String n){
if(this.name.equals(n)== true)
return true;
else return false;
}
     
//此方法用於返回貓的成員
public String toString(){
   return (this.name + "\n" + this.age + "\n" +this.color);
}
}
相關文章
相關標籤/搜索