I am just trying to look through an array and find the elements that sum up to the target number. I've gotten this far into the program:
public class App { public static void main(String[] args) throws Exception { int[] numbers = {3, 6, 2, 9}; int targNum = 5; twoSum(numbers, targNum); } public static void twoSum(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { int sum = 0; if (nums[i] <= target) { int[] sumNums = {target - nums[i]}; for (int j = 0; j < sumNums.length; j++) { sum += sumNums[j]; System.out.println(sum); } } } } } I keep getting console output of:
2 3 I ran a simple array sum in another file and it seemed to work perfectly.
https://stackoverflow.com/questions/66480625/cant-seem-to-sum-the-elements-of-this-array-and-i-have-no-idea-why March 05, 2021 at 02:04AM
没有评论:
发表评论