黑马程序员技术交流社区

标题: 球从100米高度自由落下,每次落地后反跳回原高度的一半... [打印本页]

作者: rightyuan521    时间: 2015-5-1 19:54
标题: 球从100米高度自由落下,每次落地后反跳回原高度的一半...
  1. 先第一种,为了解题而解题。

  2. ==== Main.java ====
  3. public class Main {
  4. private double TotalHeight = 100;
  5. private double CurHeight = 50;

  6. public void drop(int times) {
  7. if ((times - 1) == 0) {
  8. return;
  9. }

  10. setTotalHeight(getTotalHeight() + 2 * getCurHeight());
  11. setCurHeight(getCurHeight() / 2);

  12. drop(times - 1);
  13. }

  14. public double getTotalHeight() {
  15. return TotalHeight;
  16. }

  17. public void setTotalHeight(double totalHeight) {
  18. TotalHeight = totalHeight;
  19. }

  20. public double getCurHeight() {
  21. return CurHeight;
  22. }

  23. public void setCurHeight(double curHeight) {
  24. CurHeight = curHeight;
  25. }

  26. public static void main(String[] args) {
  27. Main main = new Main();
  28. main.drop(2);
  29. System.out.println("Total height is " + main.getTotalHeight());
  30. System.out.println("Current height is " + main.getCurHeight());
  31. }
  32. }

  33. ==== 然后是第二种 =====

  34. ==== Main.java ====
  35. package main;

  36. import javax.swing.JFrame;

  37. import panel.BallPanel;
  38. import time.Delay;

  39. public class MainFrame extends JFrame {
  40. public MainFrame(String title) {
  41. super(title);
  42. }

  43. public static void main(String[] args) {
  44. Delay delay = new Delay();

  45. MainFrame frame = new MainFrame("Hello JFrame");
  46. BallPanel ballPanel = new BallPanel();
  47. frame.add(ballPanel);
  48. frame.setSize(500, 500);
  49. frame.setVisible(true);
  50. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  51. delay.initDelay();

  52. ballPanel.setDelay(delay.getDelay());
  53. ballPanel.loopDrop(0);
  54. }
  55. }
复制代码







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2