这是一个基本问题,但我确实需要一些帮助。
给定两个整数数组a和b,如果它们具有相同的第一个元素或者它们具有相同的最后一个元素,则返回true。两个数组的长度均为1或更长。
commonEnd({1,2,3},{7,3})→true
commonEnd({1,2,3},{7,3,2})→false
commonEnd({1,2,3},{1,3})→true
我有以下代码,但它不会编译:
public boolean commonEnd(int[] a, int[] b) {
if(a[0] == b[0] || a[a.length-1] ==b[b.length-1])
return true;
}
答案 0 :(得分:4)
你错过了对if。
您需要在“else”部分返回一些内容(false)。
你应该有一个编译器错误告诉你这个。
就个人而言,我完全摆脱了if
并且做了
return a[0] == b[0] || a[a.length-1] ==b[b.length-1];
(但这可能被认为难以阅读)
答案 1 :(得分:0)
如果是不真实的话,你没有返回类型
public boolean commonEnd(int[] a, int[] b) {
if(a[0] == b[0] || a[a.length-1] ==b[b.length-1])
return true;
return false;
}
答案 2 :(得分:0)
public boolean commonEnd(int[] a, int[] b) {
if((a[0] == b[0]) || (a[a.length-1] ==b[b.length-1]))
return true;
return false;
}
你错过了return语句(如果if语句不为真),那就是它没有编译的原因。
答案 3 :(得分:0)
此代码可以正常工作:-
F:\front-end\angular-component>yarn build
yarn run v1.22.0
$ node ./scripts/build-packages-dist.js
######################################
Building release packages...
Compiling with Ivy: false
######################################
info No lockfile found.
$ node ./tools/npm/check-npm.js
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-l
ock.json.
[1/5] Validating package.json...
[2/5] Resolving packages...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
error An unexpected error occurred: "https://registry.yarnpkg.com/typescript: ESOCKETTIMEDOUT".
info If you think this is a bug, please open a bug report with the information provided in "F:\\front-end\\angular-component\\yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
INFO: Repository npm instantiated at:
no stack (--record_rule_instantiation_callstack not enabled)
Repository rule yarn_install defined at:
C:/users/chenjianlong/_bazel_chenjianlong/lo7ziovu/external/build_bazel_rules_nodejs/internal/npm_install/npm_install.bzl:434:16: in <toplevel>
ERROR: An error occurred during the fetch of repository 'npm':
yarn_install failed: (info No lockfile found.
$ node ./tools/npm/check-npm.js
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-l
ock.json.
[1/5] Validating package.json...
[2/5] Resolving packages...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
info There appears to be trouble with your network connection. Retrying...
error An unexpected error occurred: "https://registry.yarnpkg.com/typescript: ESOCKETTIMEDOUT".
info If you think this is a bug, please open a bug report with the information provided in "F:\\front-end\\angular-component\\yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
)
答案 4 :(得分:-1)
预计函数的返回语句
public boolean commonEnd(int[] a, int[] b) {
if(a[0] == b[0] || a[a.length-1] ==b[b.length-1]) //Missed `)`
{
return true;
}
return false;
}