Java 此返回语句的正确位置在哪里?

Java 此返回语句的正确位置在哪里?,java,Java,我想解决一个简单的二和问题。但是,我的问题是要弄清楚将返回数组的return语句放在哪里。如果我把return语句放在If语句中,我会得到一个缺少的return语句错误。如果将return语句放在If语句的后面,则会出现找不到符号错误。如果我把return语句放在方法末尾之前,我会得到同样的找不到符号错误。我无法添加else语句,因为如果if语句中的条件不满足,我不希望返回其他数组,因此我需要返回一个数组。似乎无论我把这个返回语句放在哪里,我都会得到一个错误。是否有一个正确的位置来放置它,或者是

我想解决一个简单的二和问题。但是,我的问题是要弄清楚将返回数组的return语句放在哪里。如果我把return语句放在If语句中,我会得到一个缺少的return语句错误。如果将return语句放在If语句的后面,则会出现找不到符号错误。如果我把return语句放在方法末尾之前,我会得到同样的找不到符号错误。我无法添加else语句,因为如果if语句中的条件不满足,我不希望返回其他数组,因此我需要返回一个数组。似乎无论我把这个返回语句放在哪里,我都会得到一个错误。是否有一个正确的位置来放置它,或者是我的实现完全错误。 我非常感谢任何人的建议。 谢谢大家!

注意:我知道这个解决方案可能无法解决二和问题,但我只想让这段代码运行,这样我就可以看到我的逻辑有什么问题,这样我就可以改进它

class Solution {
    
    public int[] twoSum(int[] nums, int target) {
        
        for (int i=0; i<nums.length; i++) {
            if(nums[i] + nums[i++] == target) {
                int[] solutions = new int[] {nums[i], nums[i++]};
                return solutions;
            }
        }
    }
}
类解决方案{
公共int[]twoSum(int[]nums,int目标){

对于(int i=0;i只需将
return
语句放入循环中,并在方法末尾添加任何
error
marker-
null
value或
throw RuntimeException()

/**
*@return数组正好有两个元素,如果找不到{@literal null}
*/
公共int[]twoSum(int[]nums,int目标){
Set unique=新的HashSet();
对于(int i=0;i
如果您的函数被定义为返回int[],那么在任何情况下都必须返回它,而不仅仅是在特定条件适用时

我不擅长Java,但就结构而言,这里有一些我认为可以在评论中解决您的问题的更改:

class Solution {
    
    public int[] twoSum(int[] nums, int target) {
        // Define here variable which contains what you would return. 
        // Give it a null, or dummy value what would be returned if your condition is not met.
           
        for (int i=0; i<nums.length; i++) {
            if(nums[i] + nums[i++] == target) {
                // Here you should assign values to the array, not create
                                int[] solutions = new int[] {nums[i], nums[i++]};
                return solutions; // remove return from here
            }
        }
        // here right before closing the function, is where return should be.
        
    }
}
类解决方案{
公共int[]twoSum(int[]nums,int目标){
//在此处定义变量,该变量包含要返回的内容。
//给它一个null或伪值,如果您的条件不满足,将返回什么。

对于(int i=0;i在没有解决方案的情况下,您希望返回什么?
null
?空数组
是否希望引发异常?调用者将期望一些返回值(即使
null`),因此不返回返回值不是一个选项。(正如您所说,您的代码还存在一些其他问题,但您很快就会发现。我认为我们没有在这里拼写这些问题是对您最大的帮助。您将从发现自己中学到更多。)谢谢你的评论。它们非常有帮助,我认为最好是我自己解决剩下的问题。谢谢你的建议。它们非常有帮助。我知道这可能不是最好的问题,但我感谢你花时间帮助初学者。我很高兴,我们在某个时候都是初学者,或者仍然是初学者其他方面;)
class Solution {
    
    public int[] twoSum(int[] nums, int target) {
        // Define here variable which contains what you would return. 
        // Give it a null, or dummy value what would be returned if your condition is not met.
           
        for (int i=0; i<nums.length; i++) {
            if(nums[i] + nums[i++] == target) {
                // Here you should assign values to the array, not create
                                int[] solutions = new int[] {nums[i], nums[i++]};
                return solutions; // remove return from here
            }
        }
        // here right before closing the function, is where return should be.
        
    }
}