你的想法不錯,但是程序邏輯有點混亂,按你的思路隨手寫了個,注釋還算比較全吧
創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的平定網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
子兔子和父兔子只要一個類就能表示,都可以抽象為兔子,并沒有實質(zhì)區(qū)別,兔子里面有屬性:父兔子對象、子兔子List、是否死了、出生月數(shù)
我沒有處理超出Integer類型范圍的異常,死期初始定為Integer.MAX_VALUE,即不會死
/**
*?Rabbit.java
*/
import?java.util.ArrayList;
import?java.util.List;
/**
*?兔子
*?
*?@author?Shawn
*?@date?Jun?30,?2013
*/
public?class?Rabbit?{
/**
?*?多少月可以生兔子(包括此月)
?*/
static?final?int?RABBIT_BORN?=?3;
/**
?*?多少月兔子會死,假設(shè)到此月兔子生下后代之后才死,值不小于RABBIT_BORN?
?*/
static?final?int?RABBIT_DEATH?=?Integer.MAX_VALUE;
/**
?*?父兔子
?*/
private?Rabbit?parent;
/**
?*?出生后有多少月
?*/
private?Integer?month;
/**
?*?死了沒
?*/
private?Boolean?isDead;
/**
?*?子兔子
?*/
private?ListRabbit?sons?=?new?ArrayListRabbit();
public?Rabbit(Rabbit?parent,?Integer?month)?{
this.parent?=?parent;
this.month?=?month;
if?(month?=?RABBIT_DEATH)?{
this.isDead?=?true;
}?else?{
this.isDead?=?false;
}
setSons();
}
private?void?setSons()?{
if?(month?=?RABBIT_BORN)?{
//?如果死了則按死期計算,否則按當(dāng)前月數(shù)計算
int?canBreedMonth?=?isDead???RABBIT_DEATH?-?RABBIT_BORN?+?1?:?month
-?RABBIT_BORN?+?1;
for?(int?i?=?0;?i??canBreedMonth;?i++)?{
Rabbit?son?=?new?Rabbit(this,?month?-?RABBIT_BORN?-?i);
sons.add(son);
}
}
}
public?Integer?getMonth()?{
return?month;
}
public?Rabbit?getParent()?{
return?parent;
}
public?Boolean?isDead()?{
return?isDead;
}
public?ListRabbit?getSons()?{
return?sons;
}
}
/**
*?RabbitTree.java
*/
import?java.util.ArrayList;
import?java.util.HashMap;
import?java.util.List;
import?java.util.Map;
/**
*?兔子的生成樹,非必要
*?其實只從一個根Rabbit對象就可計算出所有兔子個數(shù)
*?此類作用是格式化兔子樹,便于獲取樹的各種屬性
*?
*?@author?Shawn
*?@date?Jun?30,?2013
*/
public?class?RabbitTree?{
/**
?*?此后只計算有多少對兔子
?*/
static?final?int?RABBIT_PAIR?=?2;
/**
?*?多少月
?*/
private?int?month;
/**
?*?存儲整顆樹,外層Map表示層次,內(nèi)層List為每層的兔子
?*/
private?MapInteger,?ListRabbit?rabbitMap?=?new?HashMapInteger,?ListRabbit();
/**
?*?構(gòu)造函數(shù),輸入月份(包括此月)
?*?
?*?@param?month
?*/
public?RabbitTree(int?month)?{
if?(month?=?0)?{
throw?new?IllegalArgumentException("月數(shù)不正確");
}
this.month?=?month;
//?祖先兔子,從此兔子即可獲取整顆樹結(jié)構(gòu)
Rabbit?ancestor?=?new?Rabbit(null,?month);
initTree(ancestor);
}
/**
?*?樹的初始化
?*?
?*?@param?rabbit
?*/
private?void?initTree(Rabbit?rabbit)?{
ListRabbit?currentLevel?=?new?ArrayListRabbit();
ListRabbit?nextLevel?=?rabbit.getSons();
currentLevel.add(rabbit);
//處理根節(jié)點
rabbitMap.put(1,?currentLevel);
Integer?count?=?2;
ListRabbit?sons?=?new?ArrayListRabbit();
sons.addAll(nextLevel);
//處理葉節(jié)點
while?(sons.size()?!=?0)?{
currentLevel?=?new?ArrayListRabbit();
nextLevel?=?new?ArrayListRabbit();
for?(Rabbit?r?:?sons)?{
currentLevel.add(r);
for?(Rabbit?e?:?r.getSons())?{
nextLevel.add(e);
}
}
sons?=?new?ArrayListRabbit();
sons.addAll(nextLevel);
rabbitMap.put(count,?currentLevel);
count++;
}
}
/**
?*?獲得樹上兔子總數(shù)
?*?
?*?@return
?*/
public?int?getRabbitNum()?{
int?count?=?0;
for?(Integer?level?:?rabbitMap.keySet())?{
ListRabbit?list?=?rabbitMap.get(level);
for?(Rabbit?r?:?list)?{
if?(!r.isDead())?{
count++;
}
}
}
return?count?*?RABBIT_PAIR;
}
/**
?*?獲取樹的高度
?*?
?*?@return
?*/
public?int?getLevel()?{
return?rabbitMap.size();
}
/**
?*?獲取樹的月數(shù)
?*?
?*?@return
?*/
public?int?getMonth()?{
return?month;
}
public?static?void?main(String[]?args)?{
RabbitTree?tree?=?new?RabbitTree(10);
System.out.println(tree.getRabbitNum());
}
}
這道題目考察的是運用遞歸(數(shù)列)的思路去解決問題。
假設(shè)到第24個月,示例代碼如下:
public class woo {
public static void main(String args[]) {
System.out.println(fib(24));
}
private static int fib(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return fib(n - 1) + fib(n - 2);
}
}
}
擴(kuò)展資料:
斐波那契數(shù)列(Fibonacci sequence),又稱黃金分割數(shù)列、因數(shù)學(xué)家列昂納多·斐波那契(Leonardoda Fibonacci)以兔子繁殖為例子而引入,故又稱為“兔子數(shù)列”,指的是這樣一個數(shù)列:1、1、2、3、5、8、13、21、34、……。
在數(shù)學(xué)上,斐波納契數(shù)列以如下被以遞推的方法定義:F(1)=1,F(xiàn)(2)=1, F(n)=F(n-1)+F(n-2)在現(xiàn)代物理、準(zhǔn)晶體結(jié)構(gòu)、化學(xué)等領(lǐng)域,斐波納契數(shù)列都有直接的應(yīng)用,為此,美國數(shù)學(xué)會從1963年起出版了以《斐波納契數(shù)列季刊》為名的一份數(shù)學(xué)雜志,用于專門刊載這方面的研究成果。
參考資料:
百度百科:斐波那契數(shù)列
百度百科:遞歸函數(shù)
還是耐著性子給你做完了望采納。。。
第七題
/**
*?動物抽象類
*/
public?abstract?class?Animal?{
//顏色
private?String?color;
//類別
private?String?type;
//吃飯
public?abstract?void?eat();
//叫
public?abstract?void?cry();
//get?set方法省略。。。
}
/**
*?游泳的接口
*/
public?interface?Swimable?{
//游泳
public?void?swim();
}
/**
*?兔子類
*/
public?class?Rabbit?extends?Animal?{
@Override
public?void?eat()?{
System.out.println("小兔幾吃蘿卜。。。");
}
@Override
public?void?cry()?{
System.out.println("小兔幾呵呵噠。。。");
}
}
/**
* 青蛙類
*/
public?class?Frog?extends?Animal?implements?Swimable?{
public?void?swim()?{
System.out.println("青蛙會蛙泳。。。");
}
@Override
public?void?eat()?{
System.out.println("青蛙吃昆蟲。。。");
}
@Override
public?void?cry()?{
System.out.println("青蛙呱呱叫。。。");
}
}
public?class?Test?{
public?static?void?main(String[]?args)?{
//兔子
Rabbit?rabbit?=?new?Rabbit();
rabbit.eat();
rabbit.cry();
//青蛙
Frog?frog?=?new?Frog();
frog.eat();
frog.cry();
frog.swim();
}
}
第八題
/**
*?學(xué)生類
*/
public?class?Student?{
//學(xué)號
private?String?id;
//姓名
private?String?name;
//性別
private?char?gender;
//年齡
private?int?age;
public?Student(String?id,?String?name,?char?gender,?int?age)?{
this.id?=?id;
this.name?=?name;
this.gender?=?gender;
this.age?=?age;
}
//獲得學(xué)號、姓名、性別、年齡
public?String?toString()?{
return?"學(xué)號:"+id?+"??姓名:"?+?name?+?"???性別:"?+?gender?+?"??年齡:"?+?age;
}
//修改年齡
public?void?changeAge(int?range)?{
age?=?range;
}
//get?set方法省略。。。
}
public?class?Test?{
public?static?void?main(String[]?args)?{
Student?s?=?new?Student("alibb008",?"杰克馬",?'公',?18)?;
System.out.println(s.toString());
s.changeAge(50);
System.out.println(s.toString());
}
}
感慨啊。。。
public class Test {//用遞歸法計算兔子的規(guī)律
static long fib(int x){
if(x2) return (fib(x-1)+fib(x-2));
else return 1;
}
public static void main(String[] args) {
for(int i=1;i=24;i++){
long n=fib(i);
//算出的是對數(shù).要算總數(shù)的法,*2就行
System.out.println("第"+i+"個月有兔子對數(shù)為"+n);
}
}
}
1、單行(single-line)--短注釋://……
單獨行注釋:在代碼中單起一行注釋,
注釋前最好有一行空行,并與其后的代碼具有一樣的縮進(jìn)層級。如果單行無法完成,則應(yīng)采用塊注釋。
注釋格式:/*
注釋內(nèi)容
*/
行頭注釋:在代碼行的開頭進(jìn)行注釋。主要為了使該行代碼失去意義。
注釋格式://
注釋內(nèi)容
行尾注釋:尾端(trailing)--極短的注釋,在代碼行的行尾進(jìn)行注釋。一般與代碼行后空8(至少4)個格,所有注釋必須對齊。
注釋格式:代碼
+
8(至少4)個空格
+
//
注釋內(nèi)容
2、塊(block)--塊注釋:/*……*/
注釋若干行,通常用于提供文件、方法、數(shù)據(jù)結(jié)構(gòu)等的意義與用途的說明,或者算法的描述。一般位于一個文件或者一個方法的前面,起到引導(dǎo)的作用,也可以根據(jù)需要放在合適的位置。這種域注釋不會出現(xiàn)在HTML報告中。注釋格式通常寫成:
/*
*
注釋內(nèi)容
*/
3、文檔注釋:/**……*/
注釋若干行,并寫入javadoc文檔。每個文檔注釋都會被置于注釋定界符
/**......*/
...
import java.awt.*; // 導(dǎo)入相應(yīng)的awt工具組件
import java.awt.event.*;// 導(dǎo)入awt工具組件的事件類
/**
* 作用:Calc類用于演示加法計算器的功能p
* @author 網(wǎng)絡(luò)用戶
* @version 1.0 2009-6-3
* @since JDK 1.5
*/
class Calc extends WindowAdapter implements ActionListener
{
//以下聲明相關(guān)變量
Frame f; //窗口
// Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
Button b[] = new Button[10]; //10個按鈕,分別對應(yīng)數(shù)字0到9
Button be, badd, bc; //bc為=,badd為+,bc為歸0按鈕
TextField answer; //文本框answer用于顯示相加的結(jié)果
Panel p; //面板
int t1, t2,result; //t1,t2分別表示被加數(shù),加數(shù)
Button bmin, bmul, bdev; //bmin為-,bmul為*,bdev為/
String op1 = null,lastOP = null;
boolean isOK = true;
/**
* 作用:main方法為程序運行的入口
* @param args
*/
public static void main( String args[] )
{
Calc cg = new Calc(); //創(chuàng)建一個calc類的對象
cg.go(); //調(diào)用go方法,顯示加法器界面,并完成加法功能
}
/**
* 作用:go()方法用于顯示加法器界面,并完成加法功能
*/
public void go()
{
p = new Panel(); //創(chuàng)建面板的對象,10個按鈕將在此面板對象上添加和顯示
answer = new TextField("0", 15); //設(shè)置文本框的默認(rèn)值為0,文本框的長度15個字符
b[0] = new Button("0"); //以下就是那10個按鈕,按鈕上的文本就是0到9的數(shù)字
b[1] = new Button("1");
b[2] = new Button("2");
b[3] = new Button("3");
b[4] = new Button("4");
b[5] = new Button("5");
b[6] = new Button("6");
b[7] = new Button("7");
b[8] = new Button("8");
b[9] = new Button("9");
be = new Button("="); //=號
badd = new Button("+"); //+號
bc = new Button("C"); //歸0按鈕
bmin = new Button("-"); //-號
bmul = new Button("*"); //*號
bdev = new Button("/"); //除號
p.setLayout(new GridLayout(4, 4)); //將面板設(shè)置為4行3列,即12個格子,剛好可以存放地10個數(shù)字按鈕另加+號按鈕和=號按鈕,共12個按鈕
p.add(b[1]);
p.add(b[2]);
p.add(b[3]);
p.add(b[4]);
p.add(b[5]);
p.add(b[6]);
p.add(b[7]); //添加10個數(shù)字按鈕到面板上
p.add(b[8]);
p.add(b[9]);
p.add(b[0]);
for( int i = 0; i b.length; i++ )
b[i].addActionListener(this); //為10個數(shù)字按鈕添加事件監(jiān)聽器,以用于實現(xiàn)加法功能
p.add(be); //添加=號按鈕到面板上
p.add(bc); //歸0按鈕
p.add(badd); //添加+號按鈕到面板上
be.addActionListener(this); //為=號按鈕添加事件監(jiān)聽器,用于實現(xiàn)加法功能
bc.addActionListener(this); //為歸0按鈕添加事件監(jiān)聽器,用于將計算結(jié)果歸0
badd.addActionListener(this); //為+號按鈕添加事件監(jiān)聽器,用于實現(xiàn)加法功能
p.add(bmin); //-號
p.add(bmul); //*號
p.add(bdev); //除號
bmin.addActionListener(this);
bmul.addActionListener(this);
bdev.addActionListener(this);
f = new Frame("計算器"); //創(chuàng)建一個Frame對象(窗口),窗口的標(biāo)題就是calc
f.setLocation(300, 300); //窗口的彈出位置
f.setSize(300, 300); //窗口的大小為300*300像素
f.add(answer, "North"); //將文本框設(shè)置在窗口的北部(上),窗口的默認(rèn)布局為BorderLayout,即東西南北中5個位置
f.add(p, "Center"); //將面板設(shè)置在窗口的中部(中)
f.addWindowListener(this); //為窗口添加事件監(jiān)聽器,用于實現(xiàn)關(guān)閉窗口的功能
f.pack(); //此方法用于窗口到合適的大小
f.setVisible(true); //顯示窗口,為false時將不會顯示窗口
}
//在10個數(shù)字按鈕和3個功能按鈕上單擊,將觸發(fā)此方法
public void actionPerformed( ActionEvent e )
{
if( e.getSource() == bc )
{//單擊了歸0按鈕,設(shè)置為默認(rèn)值
result = 0;
lastOP = null ;
answer.setText("0"); //文本框設(shè)為0
isOK = true; //已經(jīng)經(jīng)過運算了
} else if( e.getSource() == badd || e.getSource() == bmin || e.getSource() == bmul || e.getSource() == bdev ||e.getSource() == be)
{//單擊運算符號的按鈕
op1 = e.getActionCommand();
getResult(op1);
} else
{//單擊0到9之間的任意一個數(shù)字按鈕
for( int i = 0; i b.length; i++ )
if( e.getSource() == b[i] )
{
if( isOK == true )
{
answer.setText(b[i].getActionCommand());
isOK = false;
}
else
{
answer.setText(answer.getText()+b[i].getActionCommand()); //在文本框上顯示連續(xù)單擊那幾個數(shù)字
}
}
}
}
/**
* 作用:getResult()方法用于計算出結(jié)果值,并在文本框顯示出來P
* @param op 本次操作運算符
* @param text
*/
private void getResult( String op )
{
if(answer.getText().equals("除數(shù)不能為0"))
{
answer.setText(result+"");
return;
}
if( lastOP==null )
{//沒有上一次的操作
if(op.equals("=")){
return;
}else{
result = Integer.parseInt(answer.getText());
}
} else if( lastOP.equals("+") )
{//上一次單擊了+號按鈕
result = result + Integer.parseInt(answer.getText());
answer.setText(result+""); //文本框設(shè)為0
} else if( lastOP.equals("-") )
{//單擊-號按鈕
result = result - Integer.parseInt(answer.getText());
answer.setText(result+"");//在文本框上顯示相加的結(jié)果
} else if( lastOP.equals("*") )
{//單擊*號按鈕
result = result * Integer.parseInt(answer.getText());
answer.setText(result+""); //文本框設(shè)為0
} else if( lastOP.equals("/") )
{//單擊/號按鈕
if(answer.getText().equals("0")) answer.setText("除數(shù)不能為0"); //文本框設(shè)為0
else{
result = result / Integer.parseInt(answer.getText());
answer.setText(result+""); //文本框設(shè)為0
}
}
isOK = true; //已經(jīng)經(jīng)過運算了
lastOP = op; //記下本次操作
}
//關(guān)閉窗口,觸發(fā)此方法
public void windowClosing( WindowEvent ev )
{
System.exit(0); //退出程序
}
}
分享標(biāo)題:java一對兔子代碼注釋,兔子問題java完整代碼
瀏覽地址:http://aaarwkj.com/article44/dsiegee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、、用戶體驗、電子商務(wù)、企業(yè)網(wǎng)站制作、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)