如何修复HackerRank中的“ <identifier>预期”错误

时间:2019-07-04 02:54:18

标签: java

我是Java编码的新手,但我一直坚持简单的ArrayList总和代码。我相信我的方法是正确的,但我不了解主要方法。我尝试多次运行它,但一直说我的输入ArrayList ar没有被“识别”。需要帮助!

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class Solution {

    /*
     * Complete the simpleArraySum function below.
     */


    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new 
FileWriter(System.getenv("OUTPUT_PATH")));

        int arCount = Integer.parseInt(scanner.nextLine().trim());

        int[] ar = new int[arCount];

        String[] arItems = scanner.nextLine().split(" ");

        for (int arItr = 0; arItr < arCount; arItr++) {
            int arItem = Integer.parseInt(arItems[arItr].trim());
            ar[arItr] = arItem;
        }

        int result = simpleArraySum(ar);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedWriter.close();

    }
     // Below is my code
        public static int simpleArraySum(int n, ar) { 
             /** It says that 'ar' is not identified. I tried 
   'Arraylist<Integer> 
              *  ar but it still won't work
            int sum = 0;
                for (int i = 0; i < ar.size(); i++) {
                sum += ar.get(i);
                }

            return sum;

        }



 }

它返回的是: 编译消息:

Solution.java:39: error: <identifier> expected
    public static int simpleArraySum(int n, ar) {
                                              ^
1 error

1 个答案:

答案 0 :(得分:0)

缺少方法simpleArraySum的参数类型声明。

尝试以下签名,它应该可以工作

public static int simpleArraySum(int n,int[] ar) {