如何只篩選整數(shù)

在編程中,如果需要篩選出一個(gè)序列中的整數(shù),可以使用不同的方法,這取決于你使用的編程語言和具體需求。以下是一些常見編程語言中篩選整數(shù)的示例方法: Python```pyt...
在編程中,如果需要篩選出一個(gè)序列中的整數(shù),可以使用不同的方法,這取決于你使用的編程語言和具體需求。以下是一些常見編程語言中篩選整數(shù)的示例方法:
Python
```python
定義一個(gè)包含整數(shù)和浮點(diǎn)數(shù)的列表
numbers = [1, 2, 3.5, 4, 5.5, 6]
使用列表推導(dǎo)式篩選整數(shù)
integers = [num for num in numbers if isinstance(num, int)]
print(integers) 輸出: [1, 2, 4, 6]
```
JavaScript
```javascript
// 定義一個(gè)包含整數(shù)和浮點(diǎn)數(shù)的數(shù)組
let numbers = [1, 2, 3.5, 4, 5.5, 6];
// 使用數(shù)組的filter方法篩選整數(shù)
let integers = numbers.filter(num => Number.isInteger(num));
console.log(integers); // 輸出: [1, 2, 4, 6]
```
Java
```java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 定義一個(gè)包含整數(shù)和浮點(diǎn)數(shù)的列表
List
numbers.add(1.0);
numbers.add(2.0);
numbers.add(3.5);
numbers.add(4.0);
numbers.add(5.5);
numbers.add(6.0);
// 使用循環(huán)篩選整數(shù)
List
for (Double num : numbers) {
if (num.intValue() == num) {
integers.add(num.intValue());
本文鏈接:http:///bian/350660.html
上一篇:帶“析”字的成語有哪些