- 先第一种,为了解题而解题。
- ==== 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);
- }
- }
复制代码
|
|