十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
创新互联公司是专业的兴山网站建设公司,兴山接单;提供成都网站设计、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行兴山网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
这个题目,下面我会贴出来两种代码。其实这个题目,我烦了简单计算,想搞得有趣味性一点,结果耽误了好几天时间,最后发现搞的也不好。
先第一种,为了解题而解题。
==== 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像素,这
样能多弹几次。这个程序,最后球就在那里不动了,程序不会自己退出。
上面 wuzhikun12同学写的不错,但我想还不能运行,并且还不太完善。我给个能运行的:(注意:文件名为:Test.java)
//要实现对象间的比较,就必须实现Comparable接口,它里面有个compareTo方法
//Comparable最好使用泛型,这样,无论是速度还是代码量都会减少
@SuppressWarnings("unchecked")
class Student implements ComparableStudent{
private String studentNo; //学号
private String studentName; //姓名
private double englishScore; //英语成绩
private double computerScore; //计算机成绩
private double mathScore; //数学成绩
private double totalScore; //总成绩
//空构造函数
public Student() {}
//构造函数
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;
}
//计算总成绩
public double sum() {
this.totalScore = englishScore+computerScore+mathScore;
return totalScore;
}
//计算评测成绩
public double testScore() {
return sum()/3;
}
//实现compareTO方法
@Override
public int compareTo(Student student) {
double studentTotal = student.getTotalScore();
return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);
}
//重写toString方法
public String toString(){
return "学号:"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英语成绩:"+this.getEnglishScore()+" 数学成绩:"+this.getMathScore()+" 计算机成绩:"+this.getComputerScore()+" 总成绩:"+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())) { //照现实来说,比较是不是同一个学生,应该只是看他的学号是不是相同
return true;
} else {
return false;
}
}
/*以下为get和set方法,我个人认为,totalScore的set的方法没必要要,因为它是由其它成绩计算出来的
在set方法中,没设置一次值,调用一次sum方法,即重新计算总成绩
*/
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子类学习委员类的实现
class StudentXW extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的构造函数
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子类班长类的实现
class StudentBZ extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的构造函数
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//测试类
public class Test {
public static void main(String[] args) {
//生成若干个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()+"学生的评测成绩为:"+ avgScore+"分");
}
}
}
运行结果为:
张三学生的评测成绩为:69.66666666666667分
李四学生的评测成绩为:80.5分
王五学生的评测成绩为:78.0分
李六学生的评测成绩为:98.5分
朱漆学生的评测成绩为:60.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 + " ");
}
}
}
}
}
是好使的 但是我没找到画五角星有什么规律(五角星好象不是正规图形吧?)如果还有什么要求的话 补充问题(如果是用*填充所有的东西 不包括 “ ”的话 我可以重新再给你写一个)
在 Java 中,可以通过使用 Java 的网络编程技术来实现查找附近的设备。具体的做法如下:
获取本机的 IP 地址和子网掩码,以计算出本机所在网络中的 IP 地址范围。
使用 for 循环和 InetAddress 类扫描网络中的每一个 IP 地址。
对于每一个 IP 地址,使用 isReachable() 方法判断其是否可达,如果可达则表明该 IP 地址对应的设备存在。
以下是一份简单的示例代码:
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");
}
}
}
}
请注意,这是一份示例代码,其中的扫描范围和扫描方法可能不是最佳的,根据实际需要进行修改。
你那个文字我看不懂,路由器的路由算法其实就两类:1、距离矢量算法(如RIP协议),2、链路状态协议(如OSPF协议)。路由器不一定按最短路径转发(如OSPF协议),还要看他的路径开销等参数。如果你不考虑其他因素,仅考虑最短路径,可以用Dijistra算法写。
最简单的java代码肯定就是这个了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!