前端传来两组数据,一个是最大值和最小值。比如:1,15,另一个是传需要排除的数据,比如传的3,5,7
现在要做的处理是,在1到15这15个数字中排除掉含有3,5,7的
帮你问了下GPT:
可以通过循环遍历1到15这15个数字,并判断每个数字是否含有3,5,7来完成这个处理。
以下是一个简单的 JavaScript 示例代码:
const min = 1; // 最小值
const max = 15; // 最大值
const exclude = [3, 5, 7]; // 需要排除的数据
const result = []; // 保存处理结果的数组
// 循环遍历 min 到 max 这些数字
for (let i = min; i <= max; i++) {
// 判断当前数字是否含有3,5,7
if (
i.toString().includes(exclude[0]) ||
i.toString().includes(exclude[1]) ||
i.toString().includes(exclude[2])
) {
continue; // 如果含有,则跳过当前数字,不添加到结果数组中
}
result.push(i); // 如果不含有,则将当前数字添加到结果数组中
}
console.log(result); // 打印处理后的结果
在这个示例代码中,我们定义了三个变量,分别是最小值 min、最大值 max,以及需要排除的数据 exclude,它是一个数组,包含了需要排除的数字。
然后我们定义了一个空数组 result,用来保存处理结果。
接下来使用 for 循环遍历 min 到 max 这些数字,对于每个数字,我们使用 toString() 方法将它转换成字符串,然后判断该字符串中是否包含需要排除的数字。如果包含,则使用 continue 关键字跳过当前数字,不将它添加到结果数组中;如果不包含,则将该数字添加到结果数组中。
最后,我们使用 console.log() 方法打印处理后的结果。
Java代码如下:
public class Test {
public static void main(String[] args) {
int minValue = 1;
int maxValue = 15;
int[] exclude = {3, 5, 7};
for (int i = minValue; i <= maxValue; i++) {
boolean flag = true;
for (int j = 0; j < exclude.length; j++) {
if (i == exclude[j]) {
flag = false;
break;
}
}
if (flag) {
System.out.println(i);
}
}
}
}
public static void main(String[] args) {
Integer min = 1; // 最小值
Integer max = 15; // 最大值
List<Integer> exclude =Arrays.asList(3,5,7); // 需要排除的数据
List<Integer> res = new ArrayList<>();
for (int i = min; i <=max; i++) {
if(!exclude.contains(i)){
res.add(i);
}
}
System.out.println(res);
}
```