非法启动类型以及如何在值返回方法中创建循环

时间:2014-09-23 13:21:48

标签: java methods

此方法应该在循环中将值1-10作为参数传递,并显示返回值,该值是对象在1-10秒内下降的距离。我收到错误非法启动的类型,我不知道是否重要的​​是我使用int为秒和双倍的距离,我不知道如何循环它当second1更改为second2和等等。

         public class showDistance
    {

            public static void main(String[]args)   

        {
            showDistance(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

            public static int showDistance(int second1, int second2, int second3, 
            int second4, int second5, 

int second6,
        int second7, int second8, int second9,
        int second10)
        {   
            double distance; //to hold the distance
            distance = 0.5(9.8)(second1)(second1)
            return distance;
            System.out.println("The distance the object has fallen after " + second1 + 
                " second is " + distance + "m.")
        }

        {   
            double distance; //to hold the distance
            distance = 0.5(9.8)(second2)(second2)
            return distance;
            System.out.println("The distance the object has fallen after " + second2 + 
                " seconds is " + distance + "m.")
        }

        {   
            double distance; //to hold the distance
            distance = 0.5(9.8)(second3)(second3)
            return distance;
            System.out.println("The distance the object has fallen after " + second3 + 
                " seconds is " + distance + "m.")
        }

        {   
            double distance; //to hold the distance
            distance = 0.5(9.8)(second4)(second4)
            return distance;
            System.out.println("The distance the object has fallen after " + second4 + 
                " seconds is " + distance + "m.")
        }

        {   
            double distance; //to hold the distance
            distance = 0.5(9.8)(second5)(second5)
            return distance;
            System.out.println("The distance the object has fallen after " + second5 + 
                " seconds is " + distance + "m.")
        }

        {   
            double distance; //to hold the distance
            distance = 0.5(9.8)(second6)(second6)
            return distance;
            System.out.println("The distance the object has fallen after " + second6 + 
                " seconds is " + distance + "m.")
        }

        {   
            double distance; //to hold the distance
            distance = 0.5(9.8)(second7)(second7)
            return distance;
            System.out.println("The distance the object has fallen after " + second7 + 
                " seconds is " + distance + "m.")
        }

        {   
            double distance; //to hold the distance
            distance = 0.5(9.8)(second8)(second8)
            return distance;
            System.out.println("The distance the object has fallen after " + second8 + 
                " seconds is " + distance + "m.")
        }

        {   
            double distance; //to hold the distance
            distance = 0.5(9.8)(second9)(second9)
            return distance;
            System.out.println("The distance the object has fallen after " + second9 + 
                " seconds is " + distance + "m.")
        }

        {   
            double distance; //to hold the distance
            distance = 0.5(9.8)(second10)(second10)
            return distance;
            System.out.println("The distance the object has fallen after " + second10 + 
                " seconds is " + distance + "m.")
        }
    }

3 个答案:

答案 0 :(得分:0)

我不确定我是否遵循了您想要实现的目标,但假设您希望显示每次传递给某个功能时出现的距离,您可以执行以下操作:

public class FallDistance {
    private static final double GRAVITY = 9.81;
    public static void main(String[] args) {
        showDistances(1,2,3,4,5,6,7,8,9,10);
    }
    private static void showDistances(int... times) {
        for (int time : times) {
            System.out.println(String.format("The distance the object has fallen after %d seconds is %f", time, distanceAfterTime(time)));
        }
    }
    private static double distanceAfterTime(int seconds) {
        return 0.5 * GRAVITY * seconds * seconds;
    }
}

答案 1 :(得分:0)

第二个开放式括号{非法开始声明。 第一个}括号关闭你的功能。

你说public static int并且你会给出一个双倍的返回值。编译器会给你一个错误。

public static double(int[] sec){ // a array for you 10 values
      for(int i = 0; i < sec.length; i++){
           double distance = 0.0;
           //distance = 0.5(9.8)(sec[i])(sec[i]) i don't understand your intention
           // i think you mean this
           distance = 0.5*9.8*(double)sec[i]*(double)sec[i];
      }
      return distance;
}

答案 2 :(得分:0)

抱歉,我不确定我应该如何解决这个问题,而且已发布的两个答案都包含了我想要的内容。我知道我可能应该调用一种方法,而且我们一直在做数组,所以这也是一个很酷的主意。我想有许多方法可以给猫皮肤(没有进攻猫爱好者)。我找到了这个答案并稍微调整了一下#34;梦想代码&#34;在下降的距离下,这是Gaddis问题的名称&#34;从Java开始#34;。

public class Practice
{
    public static void main (String [] args)  
    {  
        double fallingTime;  
        for(int x=1;x<11;x++)  
        {  
            System.out.println("The distance after " + x + " second/s is " + fallingDistance(x) + "m.");  
        }  
    }  
    public static double fallingDistance(double fallingTime)  
    {  
        double a=0.5, g=9.8;//0.5=1/2  
        double d=a*g*fallingTime*fallingTime;   
        return d;  
    }  
}