在 java class 中出现错误
getting errors in a java class
我对这种 java 语言很陌生,正在练习建立我的基础知识。所以这是代码和错误请告诉我如何解决这个问题
class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums.length; j++){
if(nums[i] + nums[j] == target){
return int[]{i, j};
}
}
}
return ;
}
}
Line 10: error: '.class' expected
return int[]{i, j};
^
Line 10: error: not a statement
return int[]{i, j};
^
Line 10: error: ';' expected
return int[]{i, j};
^
Line 10: error: not a statement
return int[]{i, j};
^
Line 10: error: ';' expected
return int[]{i, j};
^
5 errors
不清楚你想要什么 return 如果 nums[i] + nums[j]
永远不匹配 target
,但我认为 return 一个空数组是可以的案例:
class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums.length; j++){
if(nums[i] + nums[j] == target){
// you need to add "new"
return new int[]{i, j};
}
}
}
// you can also "return null;", or anything else
return new int[]{};
}
}
我对这种 java 语言很陌生,正在练习建立我的基础知识。所以这是代码和错误请告诉我如何解决这个问题
class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums.length; j++){
if(nums[i] + nums[j] == target){
return int[]{i, j};
}
}
}
return ;
}
}
Line 10: error: '.class' expected
return int[]{i, j};
^
Line 10: error: not a statement
return int[]{i, j};
^
Line 10: error: ';' expected
return int[]{i, j};
^
Line 10: error: not a statement
return int[]{i, j};
^
Line 10: error: ';' expected
return int[]{i, j};
^
5 errors
不清楚你想要什么 return 如果 nums[i] + nums[j]
永远不匹配 target
,但我认为 return 一个空数组是可以的案例:
class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums.length; j++){
if(nums[i] + nums[j] == target){
// you need to add "new"
return new int[]{i, j};
}
}
}
// you can also "return null;", or anything else
return new int[]{};
}
}