gpt4 book ai didi

java - 如何修复 HackerRank 中的 ' expected' 错误

转载 作者:行者123 更新时间:2023-12-02 03:31:55 26 4
gpt4 key购买 nike

我是 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

最佳答案

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

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

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

关于java - 如何修复 HackerRank 中的 '<identifier> expected' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56880087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com