java练习题

题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?

这个题目,下面我会贴出来两种代码。其实这个题目,我烦了简单计算,想搞得有趣味性一点,结果耽误了好几天时间,最后发现搞的也不好。

先第一种,为了解题而解题。

==== 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);
}
}

==== Delay.java ====
package time;

public class Delay {
public void initDelay() {
int g = 10;
int i = 0;
double s, t, t0 = 0.0;

delay = new int[100];

for (s = 100; s < 10100; s += 100) {
t = Math.sqrt(2 * s / g);
delay[i++] = (int) ((t - t0) * 100);
t0 = t;
}
}

public int[] getDelay() {
return delay;
}

private int delay[];
}

==== BallPanel.java ====
package panel;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class BallPanel extends JPanel {
public BallPanel() {
super();
}

public void paint(Graphics g) {
g.clearRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.BLUE);
g.fillOval(250, ballCenter, 30, 30);
}

public void loopDrop(int height) {
int i;

if (this.height == height) { // At bottom
for (i = 0; i < targetHeight; i += MUL) {
ballCenter = this.height - i;
this.repaint();
try {
Thread.sleep(delay[(targetHeight - i - 1) / MUL]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
loopDrop(this.height - i);
} else { // At top
for (i = height; i < this.height; i += MUL) {
ballCenter = i;
this.repaint();
try {
Thread.sleep(delay[(i - height) / MUL]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

targetHeightV = targetHeightV / 2;
targetHeight = targetHeightV;
if (targetHeight != 0) {
loopDrop(i);
}
}
}

public void setDelay(int delay[]) {
this.delay = delay;
}

private int extracted() {
return 100 * MUL;
}

private int targetHeight = extracted();

private int targetHeightV = extracted();
private int ballCenter = 0;
private int height = extracted();
private int delay[];

private final int MUL = 4;
}

其实所谓的第二种,是用的JFrame在JPanel上画图,直观的展示出来每次弹起来的效果。因为100像素实在太小了,所以我做了一个变量MUL,相当于是等比例扩大的效果。问题就是弹不到10次距离就用光了,所以。。。。。权当娱乐了。。当然可以等比例在放大,例如100M看成是10000像素,这样能多弹几次。这个程序,最后球就在那里不动了,程序不会自己退出。

猜你喜欢

转载自sjklfj22.iteye.com/blog/2206074