当前位置:网站首页>循环语句综合练习
循环语句综合练习
2022-08-02 10:15:00 【白茶清欢*】
1.从键盘输入个数不确定的整数,并判断读入的正整数和负数的个数,输入为0时结束程序.
import java.util.Scanner;
class ForWhileTest{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int positiveNumber=0;//记录正数的个数
int negativeNumber=0;//记录负数的个数
for(;;) 或者while(true){
int num=sc.nextInt();
if(num>0){
positiveNumber++;
}else if(num<0){
negativeNumber++;
}else{
break;
}
}
System.out.println("输入的正数个数为:"+positiveNumber);
System.out.println("输入的负数个数为:"+negativeNumber);
}
}
说明:
1.不在循环条件部分限制次数的结构for(;;)或者while(true)
2.结束循环有两种方式:
方式1:循环条件部分返回false
方式2:在循环体中,执行 break;
边栏推荐
- 如何封装微信小程序的 wx.request() 请求
- One Summer of Open Source | How to Quickly Integrate Log Modules in GO Language Framework
- LayaBox---TypeScript---Mixins
- 链表的实现
- Hongxing, donate another million
- 用正向迭代器封装实现反向迭代器
- R语言时间序列数据算术运算:使用log函数将时间序列数据的数值对数化、使用diff函数计算对数化后的时间序列数据的逐次差分(计算价格的对数差分)
- iNFTnews | Seeing the two sides of the metaverse, what is the true Internet and the Internet of value?
- LayaBox - TypeScript - merge statement
- yolov7创新点
猜你喜欢

21年毕业转行软件测试,从0收入到月薪过万,我真的很幸运...

8年软件测试工程师的感悟:与薪资相匹配的永远是实力

同样做软件测试,和月收入 3W 的学弟聊了一晚上,我彻底崩溃了

迭代器失效问题

瑞萨RZ/G2L处理器详细测评

一款优秀的中文识别库——ocr

List-based queuing and calling system

DVWA 通关记录 2 - 命令注入 Command Injection

阿里CTO程立:阿里巴巴开源的历程、理念和实践

Jay Chou's new song is released, crawl the "Mojito" MV barrage, and see what the fans have to say!
随机推荐
R language ggplot2 visualization: based on the fill parameter and shape parameter in the aes function, custom draw a grouped line chart and add data points (scatter points), use the legend.position fu
周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!
Shell script realizes multi-select DNS simultaneous batch resolution of domain name IP addresses (new update)
After 21 years of graduation, I switched to software testing. From 0 income to a monthly salary of over 10,000, I am really lucky...
DirectX修复工具增强版「建议收藏」
软件测试X模型
R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化水平柱状图(条形图)、使用orientation参数设置柱状图转置为条形图
Rust 从入门到精通03-helloworld
小几届的学弟问我,软件测试岗是选11k的华为还是20k的小公司,我直呼受不了,太凡尔赛了~
php组件漏洞
How to encapsulate the wx.request() request of WeChat applet
LayaBox---TypeScript---Symbols
Linux系统卸载,安装,升级,迁移clickHouse数据库
未知内容监控
用正向迭代器封装实现反向迭代器
38岁女儿不恋爱没有稳定工作老母亲愁哭
软件测试与质量 之白盒测试
LayaBox - TypeScript - merge statement
从测试入门到测试架构师,这10年,他是这样让自己成才的
3年测试在职,月薪还不足2w,最近被裁员,用亲身经历给大家提个醒...