JDK:jdk1.8.0_192
Eclipse:Eclipse JAVA Photon
明明代码是一样的吧?
但报错代码加上个:j=i+1;就不报错了:
我贴下报错的代码:
package com.Two;
class Demo01 {
public int[] twoSum(int[] nums, int target) {
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++);{
if(nums[j] == target - nums[i]){
return new int[] {i,j};
}
}
}
throw new IllegalArgumentException("No two sum solution");
}
}
这是不报错的代码:
package com.Two;
class Test{
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == target - nums[i]) {
return new int[] { i, j };
}
}
}
throw new IllegalArgumentException("No two sum solution");
}
}
是什么原因呢?你们用这代码会报错吗?