求大神帮忙,!看看
/* 题目:一球从100米高度自由落下,
* 每次落地后反跳回原高度的一半;
* 再落下,求它在 第N次落地时,共经过多少米?
* 第N次反弹多高?(将处理写到方法中,
* 并且方法需要带一个参数,该参数表示第N次落地)
*/import java.util.Scanner;
public class Test3 {
public static double method(int k)
{
double sum=100;
double h=100;
for(int i=1;i<=k;i++)
{
sum+=h/2*2;
h=h/2;
}
return sum;
}
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
System.out.println("请输入第N次数:");
int n=scanner.nextInt();
double y=method(n);
System.out.println(y);
}
}
我的方案不行!求大神帮忙!
------解决方案--------------------public class Test3 {
public static void meth(int num){
int total = 0;
int height = 100;
for (int i = 0; i < num; i++) {
total = total + height;
height = height / 2;
}
System.out.println("第" + num + "次反弹:" + height + "米 ,"+"第" + num + "次落下经过距离:" + total+"米");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第N次数:");
int n = scanner.nextInt();
meth(n);
}
}
------解决方案--------------------
public class Demo1
{
public static void main(String[] args)
{
System.out.println(method(4));
}
public static double method(int n)
{
//第一次下落距离为100,所以初始化值sum=100
double sum=100;
//i<n,因为初始化第一次值为100,即sum值为100,i=1,其实是下落第二次,所以i<n
for(int i=1;i<n;i++)
{
sum+=((100/Math.pow(2, i))*2);//每次下落弹起,所以距离乘2
}
return sum;
}
}
------解决方案--------------------这个答案网上应该有很多把.
------解决方案--------------------import java.util.Scanner;
public class TestQiu {
public static void main(String[] args) {
System.out.println("請輸入彈跳次數");
Scanner scn=new Scanner(System.in);
int num=scn.nextInt();
System.out.print("共經"+new TestQiu().getHightNum(num)+"米");
}
public double getHightNum(int n)
{
int iHightNum=100;
double total=0.0;
for(int i=1;i<=n;i++)
{
total=total+iHightNum*1.5;
iHightNum=iHightNum/2;
}
return total;
}
}