当前位置:网站首页>循环语句综合练习
循环语句综合练习
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;
边栏推荐
- currentstyle 织梦_dede currentstyle属性完美解决方案
- R语言ggpubr包的ggline函数可视化分组折线图、add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、自定义palette设置颜色
- 3 d laser slam: LeGO - LOAM - ground point extracting method and the analysis of the code
- LayaBox---TypeScript---装饰器
- 如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
- logo 图标(php图片加文字水印)
- Use compilation to realize special effects of love
- DVWA Clearance Log 2 - Command Injection
- LayaBox---TypeScript---Three slash instructions
- LayaBox---TypeScript---Namespaces and modules
猜你喜欢
随机推荐
多大数量级会出现哈希碰撞
Event object, do you know it well?
第十七章 Excel操作
The realization of the list
LayaBox---TypeScript---模块
小几届的学弟问我,软件测试岗是选11k的华为还是20k的小公司,我直呼受不了,太凡尔赛了~
周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!
鸿星尔克再捐一个亿
DVWA Clearance Log 2 - Command Injection
Unknown content monitoring
LayaBox---TypeScript---三斜线指令
练习-17
QT专题:组合会话框和文本编辑器
Turning and anti-climbing attack and defense
QT专题:事件机制event基础篇
Getting Started with SCM from Scratch (1): Summary of Background Knowledge
周鸿祎称微软抄袭 360 安全模式后发文否认;英特尔CEO基辛格回应市值被AMD超越:股价下跌是咎由自取|极客头条...
MSYS2 QtCreator Clangd code analysis can not find mm_malloc.h problem remedy
LayaBox---TypeScript---声明合并
How to encapsulate the wx.request() request of WeChat applet









