黑马程序员技术交流社区
标题:
球从100米高度自由落下,每次落地后反跳回原高度的一半...
[打印本页]
作者:
rightyuan521
时间:
2015-5-1 19:54
标题:
球从100米高度自由落下,每次落地后反跳回原高度的一半...
先第一种,为了解题而解题。
==== Main.java ====
public class Main {
private double TotalHeight = 100;
private double CurHeight = 50;
public void drop(int times) {
if ((times - 1) == 0) {
return;
}
setTotalHeight(getTotalHeight() + 2 * getCurHeight());
setCurHeight(getCurHeight() / 2);
drop(times - 1);
}
public double getTotalHeight() {
return TotalHeight;
}
public void setTotalHeight(double totalHeight) {
TotalHeight = totalHeight;
}
public double getCurHeight() {
return CurHeight;
}
public void setCurHeight(double curHeight) {
CurHeight = curHeight;
}
public static void main(String[] args) {
Main main = new Main();
main.drop(2);
System.out.println("Total height is " + main.getTotalHeight());
System.out.println("Current height is " + main.getCurHeight());
}
}
==== 然后是第二种 =====
==== Main.java ====
package main;
import javax.swing.JFrame;
import panel.BallPanel;
import time.Delay;
public class MainFrame extends JFrame {
public MainFrame(String title) {
super(title);
}
public static void main(String[] args) {
Delay delay = new Delay();
MainFrame frame = new MainFrame("Hello JFrame");
BallPanel ballPanel = new BallPanel();
frame.add(ballPanel);
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
delay.initDelay();
ballPanel.setDelay(delay.getDelay());
ballPanel.loopDrop(0);
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2