一个c 编程题目 题目

java练习题,每天一个java小程序-10
JAVA练习题,能做多少就做多少。
这个是从CSDN看到的。每天一个吧 。
【程序10】&
题目:一球从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) {
setTotalHeight(getTotalHeight() + 2 * getCurHeight());
setCurHeight(getCurHeight() / 2);
drop(times - 1);
public double getTotalHeight() {
return TotalH
public void setTotalHeight(double totalHeight) {
TotalHeight = totalH
public double getCurHeight() {
return CurH
public void setCurHeight(double curHeight) {
CurHeight = curH
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 ====
import javax.swing.JF
import panel.BallP
import time.D
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 ====
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);
public int[] getDelay() {
private int delay[];
==== BallPanel.java ====
import java.awt.C
import java.awt.G
import javax.swing.JP
public class BallPanel extends JPanel {
public BallPanel() {
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) {
if (this.height == height) { // At bottom
for (i = 0; i & targetH i += MUL) {
ballCenter = this.height -
this.repaint();
Thread.sleep(delay[(targetHeight - i - 1) / MUL]);
} catch (InterruptedException e) {
e.printStackTrace();
loopDrop(this.height - i);
} else { // At top
for (i = i & this. i += MUL) {
ballCenter =
this.repaint();
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 =
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像素,这样能多弹几次。这个程序,最后球就在那里不动了,程序不会自己退出。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。2013年6月 C/C++大版内专家分月排行榜第三
2013年 总版技术专家分年内排行榜第三
2012年 总版技术专家分年内排行榜第七
本帖子已过去太久远了,不再提供回复功能。 上传我的文档
 下载
 收藏
毕业于医学院校,在医院工作,有相对丰富的护理经验
 下载此文档
正在努力加载中...
C语言编程及上机题
下载积分:2000
内容提示:C语言编程及上机题
文档格式:DOC|
浏览次数:49|
上传日期: 09:47:21|
文档星级:
全文阅读已结束,如果下载本文需要使用
 2000 积分
下载此文档
该用户还上传了这些文档
C语言编程及上机题
官方公共微信

我要回帖

更多关于 java面向对象编程题目 的文章

 

随机推荐