猿问

两个偶数和两个奇数彼此相邻出现

给定一个整数数组,如果两个偶数和两个奇数在数组中彼此相邻出现,则打印 true


我试图使用“布尔值”来解决这个问题,但没有得到预期的结果。


int[] arr={2,4,5,7,8,9,10};

int len=arr.length;

boolean bool = true;


for (int i=0; i<len-1; i++)

{

    int n=arr[i];

    int m=arr[i+1];

    if (n%2==0 && m%2==0)

    {

        for (int j=i+2; j<len-1; j++)

        {

            int a=arr[j];

            int b=arr[j+1];

            if (a%2!=0 && b%2!=0)

            {

                bool=true;

                break;

            }

            else

                bool=false;

        }

    }

    else

    {

        for (int j=i+2; j<len-1; j++)

        {

            int a=arr[j];

            int b=arr[j+1];

            if (a%2==0 && b%2==0)

            {

                bool=true;

                break;

            }

            else

                bool=false;

        }

    }

}

System.out.println("2Odd+2Even:"+bool);

预期结果:2Odd+2Even:true


实际结果:2Odd+2Even:false


qq_笑_17
浏览 185回答 5
5回答

森栏

问题是您break;只退出内部循环。即使在找到匹配项后,您仍会继续外循环,该循环将bool在下一次迭代中设置为 false。无需太多更改,您只需添加if(bool){ &nbsp;&nbsp;&nbsp;&nbsp;break; }一旦找到匹配项,就在外循环的末尾离开它。

长风秋雁

这是一个可能的解决方案boolean checker (int[] array) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array.length <= 3) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < array.length - 3; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array[i] % 2 == 0 && array[i + 1] % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int j = i + 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array[j] % 2 != 0 && array[j + 1] % 2 != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (array[i] % 2 != 0 && array[i + 1] % 2 != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int j = i + 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array[j] % 2 == 0 && array[j + 1] % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

UYOU

class Dcoder{&nbsp;&nbsp;&nbsp; public static void main(String args[])&nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; int[] arr = {2, 4, 5, 7, 8, 9, 10};&nbsp; &nbsp; boolean bool1 = false;&nbsp; &nbsp; boolean bool2 = false;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; for(int i = 1; i < arr.length; i++){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(arr[i-1]%2 == 0 && arr[i]%2 == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool1 = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(arr[i-1]%2 != 0 && arr[i]%2 != 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool2 = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("2Odd + 2Even : " + (bool1 && bool2));&nbsp; }}

吃鸡游戏

您也可以使用位测试来完成。我扩展了布尔结构以使其更容易理解,但如果需要,它们可以全部组合成一个。请注意,低位决定了数字能否被 2 整除。&nbsp; &nbsp; &nbsp; int[] vals = { 2, 4, 5, 7, 9, 11, 10, 14&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; for (int i = 0; i < vals.length - 3; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int a = vals[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int b = vals[i + 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int c = vals[i + 2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int d = vals[i + 3];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean firstTwoOdd = ((a & b) & 1) == 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean firstTwoEven = ((a | b) & 1) == 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean secondTwoOdd = ((c & d) & 1) == 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean secondTwoEven = ((c | d) & 1) == 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean oddeven = firstTwoOdd && secondTwoEven;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean evenodd = firstTwoEven && secondTwoOdd;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.printf("(%d,%d,%d,%d) : %b%n",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;oddeven || evenodd);&nbsp; &nbsp; &nbsp; }

万千封印

您的代码比需要的要复杂一些;尝试使您的程序尽可能简单。在开始编写代码之前,请仔细考虑实现目标所需的步骤并将它们列出来。在这种情况下,您只需要检查数组中每组 4 个相邻元素。是这样的:public static void main(String[] args) {&nbsp; &nbsp; int[] a = { ... };&nbsp; &nbsp; System.out.println("2Odd+2Even:" + has2Odd2Even(a));}public static boolean has2Odd2Even(int[] a) {&nbsp; &nbsp; int end = a.length - 4;&nbsp; &nbsp; for (int i = 0; i <= end; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (odd(a[i])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (odd(a[i + 1]) && !odd(a[i + 2]) && !odd(a[i + 3])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 0 & 1 are odd, 2 & 3 are even&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else if (!odd(a[i + 1]) && odd(a[i + 2]) && odd(a[i + 3])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 0 & 1 are even, 2 & 3 are odd&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}public boolean odd(int i){&nbsp; &nbsp; return (i & 1) == 1;}
随时随地看视频慕课网APP

相关分类

Java
我要回答