简体   繁体   English

为什么会编译错误:“ public static double类型非法启动”?

[英]Why compilation error: “illegal start of type public static double”?

Why is this code wrong? 为什么此代码错误? btw i'm kinda new 顺便说一句,我有点新

import java.util.*;

public class exercicio6_5 {
    public static Scanner sc= new Scanner (System.in);

    public static void main (String args[]) {
        double a[]=new double [sc.nextInt()];
        double val;
        int i=0;
        do
        {
            System.out.println("Valor real: ");
            val=sc.nextDouble();
            a[i]=val;
            i++;
        }
            while (i<a.length);
            System.out.println("A media é: " + media(a,i));


        }
        public static double media(int [a], int i)
        {
            int soma=0;
            int x=0;
            double med;
            do
            {
                soma=soma+a[x];
                x++;
            }
            while (x<i);
            med=soma/i;
            return media;
        }


}

The compiler error is "illegal start of type public static double" why ? 编译器错误是“ public static double类型非法启动”为什么?

You have the method public static double media(int [a], int i) , this should be public static double media(double[] a, int i) since you are inputting a double array not an int array. 您有方法public static double media(int [a], int i) ,这应该是public static double media(double[] a, int i)因为您输入的是double数组而不是int数组。

Also int soma = 0 needs to be double soma = 0 because you aren't trying to add integer values, rather you are trying to add double values. 同样, int soma = 0需要为double soma = 0因为您不是要添加整数值,而是要添加双精度值。

Also, return media should be return med , since that is the variable you created. 另外, return media应为return med ,因为那是您创建的变量。

public static double media(int [a], int i)

这应该是

public static double media(int[] a, int i)

Thank you all for the answers, the program is running fine now. 谢谢大家的回答,该程序现在运行良好。 I changed some things in the end that's why it was a bit messy with med/media and int/double but the main problem was putting media(int a[],...), thank you again for the fast answers. 最后我更改了一些内容,这就是为什么med / media和int / double有点混乱,但是主要的问题是放置media(int a [],...),再次感谢您的快速回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM