在数组中查找数字是否存在

4
创建一个函数,它接受一个数字数组作为参数。如果数字7出现在数组中,返回“Boom!”。否则,返回“数组中没有数字7”。
function sevenBoom(arr) {

   if (arr.includes(7)) {

      return "Boom!"

   } 

  return "there is no 7 in the array"

}

测试

Test.assertEquals(sevenBoom([2, 6, 7, 9, 3]), "Boom!")
Test.assertEquals(sevenBoom([33, 68, 400, 5]), "there is no 7 in the array")
Test.assertEquals(sevenBoom([86, 48, 100, 66]), "there is no 7 in the array")
Test.assertEquals(sevenBoom([76, 55, 44, 32]), "Boom!")
Test.assertEquals(sevenBoom([35, 4, 9, 37]), "Boom!")

最后两个测试失败了,我认为这是因为它正在寻找一个7,而不仅仅是在数字本身中有一个7。我该如何纠正这个问题? 非重复问题 这与子字符串或字符串无关。为什么人们喜欢将事物标记为重复?

9
“arr.join().includes(7)” 可以满足你的需求,而无需繁琐的迭代。 - Jaromanda X
2
35、4、9、37,没有7。 - joyBlanks
@joyBlanks 是的,在37 - user12076510
1
包含将为您提供项目的精确匹配,而不是项目的数字/字符。您已经在JaromandaX的评论中有一个解决方案。 - joyBlanks
你只需要找出一个数是否包含“7”对吧? - Christhofer Natalius
不是字符串,是数字 7。你在哪里看到的字符串?我有什么遗漏吗?这与数组和数字有关。 - user12076510
10个回答

2

以下是一种使用正则表达式和Array.prototype.join相结合的方法,仅匹配7位数字:

[35, 4, 9, 37,7].join().match(/\b7\b/) !== null

这将在你的数组中搜索仅为7的元素并将其连接起来

/\b7\b/

然后所需的全部是:

function sevenBoom(arr) {
       var has7 = arr.join().match(/\b7\b/) !== null;
       if (has7) { return "Boom!" } 
    
      return "there is no 7 in the array"
    
    }

    console.log(sevenBoom([2, 6, 7, 9, 3], "Boom!"))
    console.log(sevenBoom([33, 68, 400, 5], "there is no 7 in the array"))
    console.log(sevenBoom([86, 48, 100, 66], "there is no 7 in the array"))
    console.log(sevenBoom([76, 55, 44, 32], "Boom!"))
    console.log(sevenBoom([35, 4, 9, 37], "Boom!"));


2

Solution without regular expressions:

function sevenBoom(arr) {
    for(let el of arr) {
        if(el.toString().split('').includes('7')) {
            return "Boom!"
        }
    }
    return "there is no 7 in the array"
}

console.log(sevenBoom([2, 6, 7, 9, 3], "Boom!"))
console.log(sevenBoom([33, 68, 400, 5], "there is no 7 in the array"))
console.log(sevenBoom([86, 48, 100, 66], "there is no 7 in the array"))
console.log(sevenBoom([76, 55, 44, 32], "Boom!"))
console.log(sevenBoom([35, 4, 9, 37], "Boom!"));


1

私有静态方法 boom(int[] numbers):

    String boom = "Boom!";
    String noseven ="there is no 7 in the array";
    String string_n;
    char[] chArray = new char[30] ;
    
    int numLenth = numbers.length;
    int y=0;
                    
    for (int j = 0 ; j < numLenth; j++)
        
    {
        
        string_n = String.valueOf(numbers[j]);
        for (int i = 0 ; i < string_n.length(); i++)
            
            
        {
            chArray[i] = string_n.charAt(i);
            
            
            
        if (chArray[i] == '7' )
            y = 1 ;
            
        }
        
        }
    
    
    

    if (y == 1)
        System.out.println(boom);
    else
        System.out.println(noseven);
    
}

公共静态无返回值主函数(String[]参数)

    int[] arrayofnum1 = {1,2,0,4,5,6,9,8,9};
    int[] arrayofnum2 = {7,17,27,5,6,3,12354,1234578};
    int[] arrayofnum3 = {12345689,6532198,65632198};
    
    
    boom(arrayofnum1);
    boom(arrayofnum2);
    boom(arrayofnum3);

}


1
一年后,但是尝试这个!
const sevenBoom = arr => /7/.test(arr) ? 'Boom!' : 'there is no 7 in the array';

不起作用,匹配到 76 和 37。 - JollyJoker

1

function sevenBoom(arr) {
    let regex = /7/g;
    if(arr.toString().match(regex)){
        return "Boom!";
    }else{
        return "there is no 7 in the array";
    }
}
console.log(sevenBoom([2, 6, 7, 9, 3]))
console.log(sevenBoom([33, 68, 400, 5]))
console.log(sevenBoom([86, 48, 100, 66]))
console.log(sevenBoom([76, 55, 44, 32]))
console.log(sevenBoom([35, 4, 9, 37]))

function sevenBoom(arr) {  
    let regex = /7/g;
    if(arr.toString().match(regex)){
        return "Boom!";
    }else{
        return "there is no 7 in the array";
    }
}
console.log(sevenBoom([2, 6, 7, 9, 3]));
console.log(sevenBoom([33, 68, 400, 5]));
console.log(sevenBoom([86, 48, 100, 66]));
console.log(sevenBoom([76, 55, 44, 32]));
console.log(sevenBoom([35, 4, 9, 37]));

0
我们也可以使用 Array.prototype.some 来实现,一旦数组中包含数字 7,它将立即返回 true:

function sevenBoom(arr) {
   if (arr.some(num => `${num}`.includes('7'))) {
      return "Boom!"
   } 
  return "there is no 7 in the array"
}

console.log(sevenBoom([2, 6, 7, 9, 3]))
console.log(sevenBoom([33, 68, 400, 5]))
console.log(sevenBoom([86, 48, 100, 66]))
console.log(sevenBoom([76, 55, 44, 32]))
console.log(sevenBoom([35, 4, 9, 37]))


num => ${num}.includes('7') 应该改为 num => num == 7,OP 不希望它匹配到 37 和 76。 - JollyJoker

0

或者,采用ES6符号的极端方式。 "找到值7":

[[2, 6, 7, 9, 3],[33, 68, 400, 5],[86, 48, 100, 66],[76, 55, 44, 32],[35, 7, 9, 37]]
.forEach(arr=>console.log(arr.toString(),arr.some(el=>el==7)?'boom, value==7':'nope'));

刚刚意识到数字7应该被视为一个数字而不是总数。"找出数字7":

[[2, 6, 7, 9, 3],[33, 68, 400, 5],[86, 48, 100, 66],[76, 55, 44, 32],[35, 7, 9, 37]]
.forEach(arr=>console.log(arr.toString(),arr.toString().match(/7/)?'boom, digit 7 found':'nope'));


0
创建一个函数,该函数接受一个数字数组作为参数。如果数组中出现数字7,则返回"Boom!";否则,返回"数组中没有7"。

const sevenBoom = (arr) => {
  let statement = "there is no 7 in the array";
  arr.forEach((element) => {
    element = element.toString();
    if (element.length === 1) {
      if (element == 7) {
        statement = "Boom!";
      }
    } else if (element.length > 1) {
      element = element.split("");
      // console.log(element);
      for (let i = 0; i < element.length; i++) {
        // console.log(typeof element[i]);
        if (element[i] == 7) {
          // console.log(typeof element[i]);
          statement = "Boom!";
        }
      }
    }
  });
  return statement;
};

// console.log(sevenBoom([1, 2, 3, 4, 5, 6, 7]));
// console.log(sevenBoom([5, 25, 77]));
// console.log(sevenBoom([1, 2, 4]));
// console.log(sevenBoom([42, 76, 55, 44, 32]));
// console.log(sevenBoom([2, 55, 60, 97, 86]));

Simple code using array's functions and loops please let me know if this was helpful to you!!


-1
package ArrayPractice;
import java.util.Scanner;
public class Question3 {
    public String sevenBoom(int [] test) {
        for(int i=0;i<test.length;i++){
            if(test[i]==7){
                return "Boom";}
            if(test[i]>=10){
                StringBuilder sb= new StringBuilder();
                sb.append(test[i]);
                String temp= sb.toString();
                for(int v=0;v<temp.length();v++){
                    char c= temp.charAt(v);
                    if(c=='7'){
                        return "Boom";
                    }}}
        }return "None of the items contain 7 within them.";}
    public static void main(String[] args) {
        System.out.println("Enter the array Range: ");
        Scanner sc= new Scanner(System.in);
        int i= sc.nextInt();
        int [] testing= new int[i];
        for(int x=0;x<testing.length;x++){
            System.out.println("Enter the array number: "+x);
            testing[x]= sc.nextInt();}
        Question3 question3= new Question3();
        System.out.println(question3.sevenBoom(testing));
    }}

创建一个函数,该函数接受一个数字数组并在数组中出现数字7时返回“Boom!”。否则,返回“数组中没有数字7”。 - Maninder

-1
package ArrayPractice;

import java.util.Scanner;

public class Question4 {
    public String sevenBoom(int[] test) {
        for (int i = 0; i < test.length; i++) {
            StringBuilder sb = new StringBuilder();
            sb.append(test[i]);
            String temp = sb.toString();
            if (temp.contains("7")) {
                return "Boom";
            }
        }
        return "None of the items contain 7 within them.";
    }

    public static void main(String[] args) {
        System.out.println("Enter the array Range: ");
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        int[] testing = new int[i];
        for (int x = 0; x < testing.length; x++) {
            System.out.println("Enter the array number: " + x);
            testing[x] = sc.nextInt();
        }
        Question4 question3 = new Question4();
        System.out.println(question3.sevenBoom(testing));
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接