java練習題,每天一個java小程式-1…__小程式

來源:互聯網
上載者:User
JAVA練習題,能做多少就做多少。http://bbs.csdn.net/topics/110067294
這個是從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) { 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像素,這樣能多彈幾次。這個程式,最後球就在那裡不動了,程式不會自己退出。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.