java hacker code_我陷入了Java的第一个hackerrank挑战
這是一個簡單的函數(shù),應(yīng)該返回數(shù)組元素(整數(shù))的總和.約束條件是,不應(yīng)有負(fù)整數(shù),并且每個元素的值應(yīng)小于1000.
public class Solution {
static int simpleArraySum(int[] ar, int arCount) {
int res=0;
if(arCount>=0){
for (int i=0; i<=arCount; i++){
if (ar[i]<1000){
res += i;
}
}
}
return res;
}
我寫的函數(shù)到此結(jié)束.其余代碼如在Hackerrank上所示
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, arCount);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
}
}
對于輸入6(數(shù)組大小)和1,2,3,4,10,11(數(shù)組元素),代碼返回21而不是31.我不明白為什么這樣做.
總結(jié)
以上是生活随笔為你收集整理的java hacker code_我陷入了Java的第一个hackerrank挑战的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python学习之路——装饰器
- 下一篇: vs2017常量文本字符串无法转换成ch