简体   繁体   English

Java,如果数组中“ x”在“ y”旁边,则返回true

[英]Java, return true if 'x' is next to 'y' in an array

This is, I imagine, a really simple problem to solve however I just can't figure it out. 我想这是一个非常简单的问题,但是我无法解决。

An array contains a list of integers and I want to return true if every number 'x' in the array is followed by the number 'y'. 一个数组包含一个整数列表,如果数组中的每个数字“ x”后跟数字“ y”,我想返回true。

So arrays with {x,3,4,y} or {x,x,y,4,5} or {5,8,x,x} would be false . 因此,具有{x,3,4,y}{x,x,y,4,5}{5,8,x,x}数组将为false

Whereas arrays with {x,y,4,1} or {x,y,5,1,x,y} would be true . 而具有{x,y,4,1}{x,y,5,1,x,y}数组为true

This is what I have tried so far: 到目前为止,这是我尝试过的:

for (int i = 0; i < nums.length-1; i++)
{
    if (nums[i] == x && nums[i+1] == y)
    {
        return true;
    }
    else
    {
        return false;
    }
}

return false;

My code however will only work for the first two elements in the array (so 0 and 1). 但是,我的代码仅适用于数组中的前两个元素(因此为0和1)。 It won't detect any integers further down in the array, so how do I do this? 它不会检测到数组后面的整数,那么我该怎么做呢?

Thank you. 谢谢。

I want to return true if every number 'x' in the array is followed by the number 'y'. 如果数组中的每个数字“ x”后跟数字“ y”,我想返回true。

You need to get rid of the else and modify the checks like so: 您需要摆脱else并像这样修改检查:

for (int i = 0; i < nums.length - 1; i++)
{
    if (nums[i] == x && nums[i + 1] != y)
    {
        return false;
    }
}

return true;

Caveats: 注意事项:

  1. This returns true if x is not present in the array. 如果数组中不存在x则返回true It's unclear from the question whether this is the behaviour you want. 从这个问题尚不清楚这是否是您想要的行为。
  2. This does not check whether x is the last element of the array. 这不会检查x是否是数组的最后一个元素。 Again, it's not entirely clear what you'd expect to happen if it is. 再次重申,如果发生的话,您所期望发生的事情还不是很清楚。
for (int i = 0; i < nums.length-1; i++) {    
    if (nums[i] == 2 && nums[i+1] != 3) {
        return false;
    }
}

return true;

This code will return False iff the 'x' number is not followed by 'y' . 如果'x'没有'y'则此代码将返回False。

for (int i = 0; i < nums.length-1; i++)
{
    if(nums[i] == 2 && nums[i+1] != 3) {
        return false;
    }
}
return true;

When the code reaches a return statement it stops executing the function and returns the specified value, that's why your for is only executed once. 当代码到达return语句时,它将停止执行该函数并返回指定的值,这就是为什么for仅执行一次的原因。

Since you want the condition to apply to ALL the elements in the array, you have to return false when you find a 2 that isn't followed by a 3. If the 2 is followed by a 3, you just keep checking the next position. 由于您希望条件适用于数组中的所有元素,因此当您发现2后面没有3时必须返回false。如果2后面有3,则只需继续检查下一个位置。

Like this: 像这样:

for (int i = 0; i < nums.length-1; i++)
{ 
     if(nums[i] == 2 && nums[i+1] != 3)
     {
          return false;
     }
}
return true;
for (int i = 0; i < nums.length-1; i++)
  {
    if(nums[i] == 2 && nums[i+1] == 3)
    {
    return true;
    }

  }
  return false;

Your Code is returning in the first iteration always. 您的代码总是在第一次迭代中返回。 because you have return written there in both If and else block. 因为您已经在If和else块中都写了返回值。

remove the else case or just the return; 删除其他情况或仅return; from else case if there is any other logic inside. 否则,如果内部还有其他逻辑。

int x = 2;
int y=3;
    for (int i = 0; i < nums.length-1; i++) {    
        if (nums[i] == x && nums[i+1] != y) {
            return false;
        }
    }
return true;

If you do not have x in the array, it will return true. 如果数组中没有x,它将返回true。 and if you have any x that does not have y after it, it will return false. 如果后面有没有y的x,它将返回false。

Try out Following 试用以下

    boolean flag = false;
      for (int i = 0; i <= (nums.length-1); i++) {
         if(nums[i] == x) {
              if (i == (nums.length-1)) {
                  if(nums[i] == x) {
                      flag = false;
                  }
              } else {
                  if(nums[i+1] == y) {
                      flag = true;
                  } else {
                      flag = false;
                      break;
                  }
              }
           }
      }
      return flag;

You can try out the following: 您可以尝试以下方法:

if(nums[nums.length - 1] == x)
  {
  return false;
  }
else
  {
  boolean flag = false;

  for(i = 0; i < nums.length - 1; i++)
    {
    if(nums[i] == x)
      {
      if(nums[i + 1] == y)
        {
        flag = true;
        }
      else
        {
        flag = false;
        break;
        }
      }
    }

  return flag;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM