当前位置:网站首页>循环语句综合练习
循环语句综合练习
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;
边栏推荐
- QT专题:自定义部件
- DVWA 通关记录 2 - 命令注入 Command Injection
- Event object, do you know it well?
- Do you agree with this view?Most businesses are digitizing just to ease anxiety
- 牛客刷题——剑指offer(第三期)
- TimerTask(addin timer语音)
- 身为程序猿——谷歌浏览器的这些骚操作你真的废吗!【熬夜整理&建议收藏】[通俗易懂]
- LayaBox---TypeScript---声明合并
- 8月份的.NET Conf 活动 专注于 .NET MAUI
- List-based queuing and calling system
猜你喜欢
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
Weak yen turns game consoles into "financial products" in Japan: scalpers make big profits
npm ERR! 400 Bad Request - PUT xxx - Cannot publish over previously published version “1.0.0“.
QT专题:自定义部件
后管实现面包屑功能
转转反爬攻防战
多大数量级会出现哈希碰撞
软件测试之发现和解决bug
MSYS2 QtCreator Clangd 代码分析找不到 mm_malloc.h的问题补救
同样做软件测试,和月收入 3W 的学弟聊了一晚上,我彻底崩溃了
随机推荐
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
3年测试在职,月薪还不足2w,最近被裁员,用亲身经历给大家提个醒...
软件测试岗位巨坑?阿里在职7年测试人告诉你千万别上当
[Science of Terminology] For those difficult words about the integrated workbench, read this article to understand in seconds!
【术语科普】关于集成工作台那些难懂的词儿,看这篇秒懂!
win10打印服务无法启动(运行时错误automation)
DVWA Clearance Log 2 - Command Injection
The realization of the list
牛客刷题——剑指offer(第三期)
如何封装微信小程序的 wx.request() 请求
食品安全 | 鱼肝油不是鱼油,家有宝宝的注意了
行为型模式-模板方法模式
8月份的.NET Conf 活动 专注于 .NET MAUI
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
小几届的学弟问我,软件测试岗是选11k的华为还是20k的小公司,我直呼受不了,太凡尔赛了~
开源一夏 | GO语言框架中如何快速集成日志模块
wireshark的安装教程(暖气片安装方法图解)
软件测试与质量 之白盒测试
从测试入门到测试架构师,这10年,他是这样让自己成才的
如何选择一块真正“好用的、性能高”的远程控制软件