Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类型的非法开始以及如何在值返回方法中进行循环_Java_Methods - Fatal编程技术网

Java 类型的非法开始以及如何在值返回方法中进行循环

Java 类型的非法开始以及如何在值返回方法中进行循环,java,methods,Java,Methods,此方法假定将值1-10作为参数在循环中传递,并显示返回值,即对象在1-10秒内下落的距离。我得到了类型非法开始的错误,我不知道我用int表示秒,用double表示距离是否重要,我不知道当second1变为second2时如何循环它,依此类推 public class showDistance { public static void main(String[]args) { showDistanc

此方法假定将值1-10作为参数在循环中传递,并显示返回值,即对象在1-10秒内下落的距离。我得到了类型非法开始的错误,我不知道我用int表示秒,用double表示距离是否重要,我不知道当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.")
        }
    }

我不确定我是否遵循了您想要实现的目标,但假设您想要显示在传递给函数的每个时间点上的距离,您可以执行以下操作:

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;
    }
}

语句的非法开头出现在第二个空括号{。 第一个}括号关闭函数

你说
publicstaticint
,你会给出一个double作为返回值。编译器将给您一个错误

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;
}
publicstaticdouble(int[]秒){//10个值的数组
对于(int i=0;i
很抱歉,我不确定该如何着手解决这个问题,而且已经发布的两个答案都包含了我想要的内容。我知道我可能应该调用一个方法,我们一直在做数组,所以这也是一个很酷的想法。我想有很多方法可以剥一只猫的皮(不冒犯爱猫的人)。我找到了这个答案,并在“代码中的梦想”中对其进行了稍加修改,这是Gaddis“从Java开始”中问题的名称

公共课堂实践
{
公共静态void main(字符串[]args)
{  
双下降时间;

对于(int x=1;xIs这是Java吗?在这种情况下,我想你需要阅读一些关于Java程序结构的教程。是的,我已经开始阅读一本,并且意识到我的代码中没有主方法确保平方这些秒:
返回0.5*重力*秒*秒;
你不需要为所有内容添加括号。
d=a*g*fallingTime*fallingTime
至少同样清晰和正确。与返回d
相同。
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;  
    }  
}