題目:一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地時(shí),共經(jīng)過多少米?第10次反彈多高?
創(chuàng)新互聯(lián)公司是專業(yè)的興山網(wǎng)站建設(shè)公司,興山接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行興山網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
這個(gè)題目,下面我會(huì)貼出來兩種代碼。其實(shí)這個(gè)題目,我煩了簡(jiǎn)單計(jì)算,想搞得有趣味性一點(diǎn),結(jié)果耽誤了好幾天時(shí)間,最后發(fā)現(xiàn)搞的也不好。
先第一種,為了解題而解題。
==== 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;
}
其實(shí)所謂的第二種,是用的JFrame在JPanel上畫圖,直觀的展示出來每次彈起來的效果。因?yàn)?00像素實(shí)在太小了,所以我做了一個(gè)變量MUL,相
當(dāng)于是等比例擴(kuò)大的效果。問題就是彈不到10次距離就用光了,所以。。。。。權(quán)當(dāng)娛樂了。。當(dāng)然可以等比例在放大,例如100M看成是10000像素,這
樣能多彈幾次。這個(gè)程序,最后球就在那里不動(dòng)了,程序不會(huì)自己退出。
上面 wuzhikun12同學(xué)寫的不錯(cuò),但我想還不能運(yùn)行,并且還不太完善。我給個(gè)能運(yùn)行的:(注意:文件名為:Test.java)
//要實(shí)現(xiàn)對(duì)象間的比較,就必須實(shí)現(xiàn)Comparable接口,它里面有個(gè)compareTo方法
//Comparable最好使用泛型,這樣,無論是速度還是代碼量都會(huì)減少
@SuppressWarnings("unchecked")
class Student implements ComparableStudent{
private String studentNo; //學(xué)號(hào)
private String studentName; //姓名
private double englishScore; //英語成績(jī)
private double computerScore; //計(jì)算機(jī)成績(jī)
private double mathScore; //數(shù)學(xué)成績(jī)
private double totalScore; //總成績(jī)
//空構(gòu)造函數(shù)
public Student() {}
//構(gòu)造函數(shù)
public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
this.studentNo = studentNo;
this.studentName = studentName;
this.englishScore = englishSocre;
this.computerScore = computerScore;
this.mathScore = mathScore;
}
//計(jì)算總成績(jī)
public double sum() {
this.totalScore = englishScore+computerScore+mathScore;
return totalScore;
}
//計(jì)算評(píng)測(cè)成績(jī)
public double testScore() {
return sum()/3;
}
//實(shí)現(xiàn)compareTO方法
@Override
public int compareTo(Student student) {
double studentTotal = student.getTotalScore();
return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);
}
//重寫toString方法
public String toString(){
return "學(xué)號(hào):"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英語成績(jī):"+this.getEnglishScore()+" 數(shù)學(xué)成績(jī):"+this.getMathScore()+" 計(jì)算機(jī)成績(jī):"+this.getComputerScore()+" 總成績(jī):"+this.getTotalScore();
}
//重寫equals方法
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(!(obj instanceof Student)){
return false;
}
Student student = (Student)obj;
if(this.studentNo.equals(student.getStudentName())) { //照現(xiàn)實(shí)來說,比較是不是同一個(gè)學(xué)生,應(yīng)該只是看他的學(xué)號(hào)是不是相同
return true;
} else {
return false;
}
}
/*以下為get和set方法,我個(gè)人認(rèn)為,totalScore的set的方法沒必要要,因?yàn)樗怯善渌煽?jī)計(jì)算出來的
在set方法中,沒設(shè)置一次值,調(diào)用一次sum方法,即重新計(jì)算總成績(jī)
*/
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
sum();
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
sum();
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
sum();
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
sum();
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
sum();
}
public double getTotalScore() {
return totalScore;
}
}
//Student子類學(xué)習(xí)委員類的實(shí)現(xiàn)
class StudentXW extends Student {
//重寫父類Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的構(gòu)造函數(shù)
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子類班長(zhǎng)類的實(shí)現(xiàn)
class StudentBZ extends Student {
//重寫父類Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的構(gòu)造函數(shù)
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//測(cè)試類
public class Test {
public static void main(String[] args) {
//生成若干個(gè)student類、StudentXW類、StudentBZ類
Student student1 = new Student("s001","張三",70.5,50,88.5);
Student student2 = new Student("s002","李四",88,65,88.5);
Student student3 = new Student("s003","王五",67,77,90);
StudentXW student4 = new StudentXW("s004","李六",99,88,99.5);
StudentBZ student5 = new StudentBZ("s005","朱漆",56,65.6,43.5);
Student[] students = {student1,student2,student3,student4,student5};
for(int i = 0 ; istudents.length; i++){
double avgScore = students[i].testScore();
System.out.println(students[i].getStudentName()+"學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?+ avgScore+"分");
}
}
}
運(yùn)行結(jié)果為:
張三學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?9.66666666666667分
李四學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?0.5分
王五學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?8.0分
李六學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?8.5分
朱漆學(xué)生的評(píng)測(cè)成績(jī)?yōu)椋?0.03333333333333分
public class TestStar {
public static void main(String[] args) {
String star = "*";
for (int i = 0; i 5; i++) {
if (i == 0) {
System.out.print(" " + star);
System.out.println();
}
if (i == 1) {
for (int z = 0; z 4; z++) {
System.out.print(" " + star);
}
System.out.println();
}
if (i == 2) {
System.out.print(" ");
for (int x = 0; x 3; x++) {
System.out.print(" " + star);
}
System.out.println();
}
if (i == 3) {
for (int y = 0; y 2; y++) {
System.out.print(" " + star + " ");
}
}
}
}
}
是好使的 但是我沒找到畫五角星有什么規(guī)律(五角星好象不是正規(guī)圖形吧?)如果還有什么要求的話 補(bǔ)充問題(如果是用*填充所有的東西 不包括 “ ”的話 我可以重新再給你寫一個(gè))
在 Java 中,可以通過使用 Java 的網(wǎng)絡(luò)編程技術(shù)來實(shí)現(xiàn)查找附近的設(shè)備。具體的做法如下:
獲取本機(jī)的 IP 地址和子網(wǎng)掩碼,以計(jì)算出本機(jī)所在網(wǎng)絡(luò)中的 IP 地址范圍。
使用 for 循環(huán)和 InetAddress 類掃描網(wǎng)絡(luò)中的每一個(gè) IP 地址。
對(duì)于每一個(gè) IP 地址,使用 isReachable() 方法判斷其是否可達(dá),如果可達(dá)則表明該 IP 地址對(duì)應(yīng)的設(shè)備存在。
以下是一份簡(jiǎn)單的示例代碼:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class FindDevices {
public static void main(String[] args) throws UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
String hostAddress = localHost.getHostAddress();
String subnet = hostAddress.substring(0, hostAddress.lastIndexOf(".") + 1);
for (int i = 1; i 256; i++) {
String host = subnet + i;
try {
InetAddress address = InetAddress.getByName(host);
if (address.isReachable(1000)) {
System.out.println(host + " is reachable");
}
} catch (Exception e) {
System.out.println(host + " is not reachable");
}
}
}
}
請(qǐng)注意,這是一份示例代碼,其中的掃描范圍和掃描方法可能不是最佳的,根據(jù)實(shí)際需要進(jìn)行修改。
你那個(gè)文字我看不懂,路由器的路由算法其實(shí)就兩類:1、距離矢量算法(如RIP協(xié)議),2、鏈路狀態(tài)協(xié)議(如OSPF協(xié)議)。路由器不一定按最短路徑轉(zhuǎn)發(fā)(如OSPF協(xié)議),還要看他的路徑開銷等參數(shù)。如果你不考慮其他因素,僅考慮最短路徑,可以用Dijistra算法寫。
最簡(jiǎn)單的java代碼肯定就是這個(gè)了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是應(yīng)該是所有學(xué)java的新手看的第一個(gè)代碼了。如果是零基礎(chǔ)的新手朋友們可以來我們的java實(shí)驗(yàn)班試聽,有免費(fèi)的試聽課程幫助學(xué)習(xí)java必備基礎(chǔ)知識(shí),有助教老師為零基礎(chǔ)的人提供個(gè)人學(xué)習(xí)方案,學(xué)習(xí)完成后有考評(píng)團(tuán)進(jìn)行專業(yè)測(cè)試,幫助測(cè)評(píng)學(xué)員是否適合繼續(xù)學(xué)習(xí)java,15天內(nèi)免費(fèi)幫助來報(bào)名體驗(yàn)實(shí)驗(yàn)班的新手快速入門java,更好的學(xué)習(xí)java!