Java实现循环体的过滤器的方法
时间:2023-11-22 09:35:33
编写程序,利用continue语句实现循环体过滤器,过滤“老鹰”字符串,并做相应的处理,但是放弃continue语句之后的所有代码。即若遇到“老鹰”字符串则进行特定处理,然后使用continue语句跳过一般处理。
public class Continue {
public static void main(String[] args){
String[] array = new String[] { "白鹭", "丹顶鹤", "黄鹂", "鹦鹉", "乌鸦", "喜鹊",
"老鹰", "布谷鸟", "老鹰", "灰纹鸟", "老鹰", "百灵鸟" };
System.out.println("在我的花园里有很多鸟类,但是最近来了几只老鹰,请帮我把它们抓走。");
int eagleCount = 0;
for (String string : array) {// foreach遍历数组
if (string.equals("老鹰")) {// 如果遇到老鹰
System.out.println("发现一只老鹰,已经抓到笼子里。");
eagleCount++;
continue;// 中断循环
}
System.out.println("搜索鸟类,发现了:" + string);// 否则输出数组元素
}
System.out.println("一共捉到了:" + eagleCount + "只老鹰。");
}
}
效果如图所示:
标签:Java,循环体,过滤器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
挑战4道Java试题
2022-04-12 04:41:02
学习Java内存模型JMM心得
2023-11-28 20:56:17
![](https://img.aspxhome.com/file/2023/4/59974_0s.png)
Java8新特性之默认方法(default)浅析
2023-10-03 10:41:13
![](https://img.aspxhome.com/file/2023/1/62051_0s.png)
深入理解Java设计模式之抽象工厂模式
2023-11-28 12:11:34
![](https://img.aspxhome.com/file/2023/6/60336_0s.jpg)
Mybatis如何使用ognl表达式实现动态sql
2021-06-22 03:34:49
IDEA 2022 中的Lombok 使用基础教程
2023-04-09 21:57:09
![](https://img.aspxhome.com/file/2023/7/64547_0s.png)
Android TextView 设置字体大小的方法
2022-06-18 03:05:25
Android SlidingDrawer 抽屉效果的实现
2023-08-02 07:58:30
![](https://img.aspxhome.com/file/2023/5/88665_0s.jpg)
c#获取相同概率随机数的算法代码
2022-09-07 21:18:13
Springcloud Eureka配置及集群代码实例
2021-09-19 05:54:52
Java KindEditor粘贴图片自动上传到服务器功能实现
2023-08-07 01:42:33
![](https://img.aspxhome.com/file/2023/2/57902_0s.gif)
关于@ApiImplicitParams、ApiImplicitParam的使用说明
2023-11-09 10:49:34
![](https://img.aspxhome.com/file/2023/0/59220_0s.jpg)
Java设置PDF有序和无序列表的知识点总结
2022-04-18 03:29:27
![](https://img.aspxhome.com/file/2023/0/91130_0s.png)
C#引用类型和值类型的介绍与实例
2022-08-11 16:24:34
![](https://img.aspxhome.com/file/2023/7/100087_0s.jpg)
C#使用xsd文件验证XML格式是否正确的实现方法
2022-02-08 11:11:09
详解Java 中的三种代理模式
2023-06-25 07:49:17
JAVA操作MongoDB数据库实例教程
2023-11-18 13:22:27
c#高效率导出多维表头excel的实例代码
2022-10-17 04:45:49
Android实现文字垂直滚动、纵向走马灯效果的实现方式汇总
2023-03-02 22:24:54
![](https://img.aspxhome.com/file/2023/8/93298_0s.gif)
使用位运算实现网页中的过滤、筛选功能实例
2023-06-04 10:00:31
![](https://img.aspxhome.com/file/2023/3/111253_0s.jpg)