检查数组是否包含两个数字

huangapple go评论68阅读模式
英文:

Checking if an array contains two numbers

问题

import java.util.Scanner;

public class checker {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int length = scanner.nextInt();
        int[] numbers = new int[length];

        boolean broken = false;
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = scanner.nextInt();
        }
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        for (int j = 1; j < length; j++) {

            if (numbers[j] == n && numbers[j-1] == m || numbers[j] == m && numbers[j+1] == n || numbers[j] == m && numbers[j-1] == n || numbers[j] == n && numbers[j+1] == m) {
                broken = false;
            } else {
                broken = true;
            }

        }
        if (broken) {
            System.out.println("false");
        } else {
            System.out.println("true");
        }
    }
}
英文:

i have just started with Java and facing a problem right now, which i cannot solve after 2hrs of checking code.
When i input :
7 ( as length)
6 3 4 8 3 2 6 (as array)
8 3 (as numbers to check)

it Writes false, but clearly they are included in the exact order.
Please help me trying to understand what the problem is

here is the Code:


import java.util.Scanner;

public class checker {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int length = scanner.nextInt();
        int[] numbers = new int[length];

        boolean broken = false;
        for (int i = 0; i &lt; numbers.length; i++) {
            numbers[i] = scanner.nextInt();
        }
        int n = scanner.nextInt();
        int m = scanner.nextInt();

        for (int j = 1; j &lt; length; j++) {

            if (numbers[j] == n &amp;&amp; numbers[j-1] == m || numbers[j] == m &amp;&amp; numbers[j+1] == n || numbers[j] == m &amp;&amp; numbers[j-1] == n || numbers[j] == n &amp;&amp; numbers[j+1] == m) {
                broken = false;
            } else {
                broken = true;
            }

        }
        if (broken) {
            System.out.println(&quot;false&quot;);
        } else {
            System.out.println(&quot;true&quot;);
        }
    }
}


</details>


# 答案1
**得分**: 0

你在评论中提到,你想要检查 {n,m} 和 {m,n},那么为什么要增加更多的条件呢?下面的代码足够了:

```java
for (int j = 0; j < length-1; j++) {
    if (numbers[j] == n && numbers[j+1] == m || numbers[j] == m && numbers[j+1] == n) {
        broken = false;
        break;
    } else {
        broken = true;
    }
}

循环从 0(第一个数组元素)到最后 -1,因为 if 语句与 j+1 进行比较。一旦找到匹配项,必须跳出循环,否则可能会再次将 broken 设置为 false。

英文:

As you mentioned in a comment, you want to check on {n,m} and {m,n} so why do you have more conditions?
The following is enough:

for (int j = 0; j &lt; length-1; j++) {
    if (numbers[j] == n &amp;&amp; numbers[j+1] == m || numbers[j] == m &amp;&amp; numbers[j+1] == n) {
        broken = false;
        break;
    } else {
        broken = true;
    }
}

The loop goes from 0 (=first array element) to the last -1 because the if compares with j+1. Once you have found a match you have to break out of the loop otherwise you risk to set broken to false again.

答案2

得分: 0

你也可以在不使用布尔值的情况下实现这一点,查看下面调整后的代码。

import java.util.Scanner;

public class checker {

    public static void main(String[] args) {

        int howManyValuesPresent = 0;

        Scanner scanner = new Scanner(System.in);
        System.out.println("数组长度");
        int length = scanner.nextInt();
        int[] numbers = new int[length];

        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = scanner.nextInt();
        }

        int n = scanner.nextInt();
        int m = scanner.nextInt();

        for(int number: numbers){
            if(number == n || number == m){
                howManyValuesPresent++;
            }
        }

        if (howManyValuesPresent > 0) {
            System.out.println("true,你选择的数字出现:" + howManyValuesPresent + " 次在数组中");
        } else {
            System.out.println("false,你选择的数字出现:" + howManyValuesPresent + " 次在数组中");
        }
    }
}
英文:

You could also achieve this without using a boolean, check tweaked code below.

import java.util.Scanner;

public class checker {

public static void main(String[] args) {

    int howManyValuesPresent = 0;

    Scanner scanner = new Scanner(System.in);
    System.out.println(&quot;Length of array&quot;);
    int length = scanner.nextInt();
    int[] numbers = new int[length];

    for (int i = 0; i &lt; numbers.length; i++) {
        numbers[i] = scanner.nextInt();
    }


    int n = scanner.nextInt();

    int m = scanner.nextInt();


    for(int number: numbers){
        if(number == n || number == m){
            howManyValuesPresent++;
        }
    }

    if (howManyValuesPresent&gt;0) {
        System.out.println(&quot;true, Your selected numbers appear: &quot; + howManyValuesPresent + &quot; times in array&quot;);
    } else {
        System.out.println(&quot;false, Your selected numbers appear: &quot; + howManyValuesPresent + &quot; times in array&quot;);
    }
}
}

huangapple
  • 本文由 发表于 2020年9月16日 16:39:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63916273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定