当前位置:网站首页>循环语句综合练习
循环语句综合练习
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;
边栏推荐
- MSYS2 QtCreator Clangd code analysis can not find mm_malloc.h problem remedy
- ConvNeXt论文及实现
- Unknown content monitoring
- 3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
- The ggline function of the R language ggpubr package visualizes grouped line graphs, the add parameter is mean_se and dotplot to visualize line graphs of different level averages, and adds error bars
- 多大数量级会出现哈希碰撞
- 日元疲软令游戏机在日本变身“理财产品”:黄牛大赚
- [Science of Terminology] For those difficult words about the integrated workbench, read this article to understand in seconds!
- 从测试入门到测试架构师,这10年,他是这样让自己成才的
- yolov7创新点
猜你喜欢
随机推荐
8月份的.NET Conf 活动 专注于 .NET MAUI
3年测试在职,月薪还不足2w,最近被裁员,用亲身经历给大家提个醒...
The 38-year-old daughter is not in love and has no stable job, the old mother is crying
You Only Hypothesize Once: 用旋转等变描述子估计变换做点云配准(已开源)
软件测试X模型
LayaBox---TypeScript---迭代器和生成器
R语言ggplot2可视化:使用ggpubr包的ggbarplot函数可视化水平柱状图(条形图)、使用orientation参数设置柱状图转置为条形图
LayaBox---TypeScript---模块解析
阿里CTO程立:阿里巴巴开源的历程、理念和实践
38岁女儿不恋爱没有稳定工作老母亲愁哭
c#反射和特性
wireshark的安装教程(暖气片安装方法图解)
Event 对象,你很了解吗?
第十五章 多线程
用正向迭代器封装实现反向迭代器
全方位剖析Numpy中的np.diag源代码
21年毕业转行软件测试,从0收入到月薪过万,我真的很幸运...
The R language uses the rollapply function in the zoo package to apply the specified function to the time series in a rolling manner and the window moves, and set the align parameter to specify that t
This article takes you to understand the commonly used models and frameworks of recommender systems
LayaBox---TypeScript---JSX








