循环语句

循环语句:

当我们要做一些重复的操作时,首先想到的是有没有一种循环的语句?

答案当然有

Java提供了三种循环:

  1. for循环,在Java5中引入了一种主要用于数组的增强型for循环。
  2. while循环
  3. do……while循环

for循环语法1:

for(初始化值,boolean值,改变初始值){
//循环要执行的代码
}

for循环执行的次数是在执行前就确定的。

实例:打印1~10

public class Mian {public static void main(String[] args) {// TODO Auto-generated method stubfor(int i=1;i<=10;i++) {System.out.println(i);}}}

控制台:打印1~10

for循环语法2:

for(数据类型 变量名:被遍历的数组或集合){
}

实例遍历数组:

public class Mian {public static void main(String[] args) {// TODO Auto-generated method stubint[] arrays= {1,2,3,4,5,6,7,8,9,10};for(int arrays2:arrays) {System.out.println(arrays2);}}}

控制台:打印1~10

 

while语法:

while(boolean类型){
//如果为true则执行
}

只要布尔表达式为 true,循环就会一直执行下去

实例:使用while打印1~10

public class Mian {public static void main(String[] args) {// TODO Auto-generated method stubint x=1;while(x<=10) {System.out.println(x);x++;}}}

控制台打印:1~10

do……while语法:

do{
//执行的代码
}
while(boolean类型)

对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。

do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。

实例:你还跑步吗?

import java.util.Scanner;public class Mian {public static void main(String[] args) {// TODO Auto-generated method stubScanner in=new Scanner(System.in);int a = 1;//1代表还跑步//0代表不跑步do {System.out.println("你还跑步吗");if (in.hasNext()) {a=in.nextInt();}}while(a==1);if (a==0) {System.out.println("跑不动");}}}

 

控制台输出:

你还跑步吗
1
你还跑步吗
1
你还跑步吗
0
跑不动

break关键字:

它是用在switch或者是循环语句中的,当程序执行到break时则会立马跳出循环体或者是switche。

语法格式:

break

实例:使用循环语句和break写跑步程序

import java.util.Scanner;public class Mian {public static void main(String[] args) {// TODO Auto-generated method stubScanner in = new Scanner(System.in);int a = 1;// 1代表还跑步// 0代表不跑步System.out.println("开始跑步");while (a == 1) {System.out.println("你还跑步吗");if (in.hasNextInt()) {a = in.nextInt();}if (a == 0) {System.out.println("跑不动了");break;}}}}

控制台结果:

开始跑步
你还跑步吗
1
你还跑步吗
0
跑不动了

continue关键字:

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

在 for 循环中,continue 语句使程序立即跳转到初始化值改变哪里(到这里i++)。

在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。

continue语法:

continue

 

实例:跳过数组的30

public class Mian {public static void main(String[] args) {// TODO Auto-generated method stubint[] arrays = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };for(int arrays2:arrays) {if (arrays2==30) {System.out.println("我要跳过30");continue;//如果遍历到了数组元素30则跳到循环语句再遍历
            }System.out.println(arrays2);}}}

控制台打印:

10
20
我要跳过30
40
50
60
70
80
90

转载于:https://www.cnblogs.com/ahJava/p/9787020.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388227.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

canva怎么使用_使用Canva进行数据可视化项目的4个主要好处

canva怎么使用(Notes: All opinions are my own. I am not affiliated with Canva in any way)(注意&#xff1a;所有观点均为我自己。我与Canva毫无关系) Canva is a very popular design platform that I thought I would never use to create the deliverable for a Data V…

如何利用Shader来渲染游戏中的3D角色

杨航最近在学Unity3D&#xfeff;&#xfeff; 本文主要介绍一下如何利用Shader来渲染游戏中的3D角色&#xff0c;以及如何利用Unity提供的Surface Shader来书写自定义Shader。 一、从Shader开始 1、通过Assets->Create->Shader来创建一个默认的Shader&#xff0c;并取名…

深入bind

今天来聊聊bind 关于之前的call跟apply 查看此链接 我们要明确4点内容 1. bind之后返回一个函数 let obj {name : skr } function fn(){console.log(this) } let bindfn fn.bind(obj) console.log(typeof bindfn) // function 2.bind改变this 并且可以传参 bind之后的函数仍…

Css单位

尺寸 颜色 转载于:https://www.cnblogs.com/jsunny/p/9866679.html

ai驱动数据安全治理_JupyterLab中的AI驱动的代码完成

ai驱动数据安全治理As a data scientist, you almost surely use a form of Jupyter Notebooks. Hopefully, you have moved over to the goodness of JupyterLab with its integrated sidebar, tabs, and more. When it first launched in 2018, JupyterLab was great but fel…

【Android】Retrofit 2.0 的使用

一、概述 Retrofit是Square公司开发的一个类型安全的Java和Android 的REST客户端库。来自官网的介绍&#xff1a; A type-safe HTTP client for Android and JavaRest API是一种软件设计风格&#xff0c;服务器作为资源存放地。客户端去请求GET,PUT, POST,DELETE资源。并且是无…

一个透明的shader

杨航最近在学Unity3D&#xfeff;&#xfeff;Shader "Custom/xiankuang" { Properties { _LineColor ("Line Color", Color) (1,1,1,1) _GridColor ("Grid Color", Color) (1,1,1,0) _LineWidth ("Line Width", float) 0…

Mysql常用命令(二)

对数据库的操作 增 create database db1 charset utf8; 查 # 查看当前创建的数据库 show create database db1; # 查看所有的数据库 show databases; 改 alter database db1 charset gbk; 删 drop database db1; 对表的操作 use db1; #切换文件夹select database(); #查看当前所…

python中定义数据结构_Python中的数据结构—简介

python中定义数据结构You have multiples algorithms, the steps of which require fetching the smallest value in a collection at any given point of time. Values are assigned to variables but are constantly modified, making it impossible for you to remember all…

1206封装电容在物料可靠性设计比较低

1206封装电容在物料可靠性设计中是要尽力避免的&#xff0c;尽量选择0805或1210。在现场中容易出现电容因断裂而击穿的情况。同时容易造成保险丝烧断。转载于:https://www.cnblogs.com/conglinlixian/p/10414877.html

Java开发中 Double 和 float 不能直接运算

不能直接运算 是因为计算机储存浮点类型的数值使用指数和尾数来表示 这就意味着计算时会出现“精度缺失”的现象 为了解决这个问题 我们引入 java.math.BigDecimal类来进行精确计算。 具体如下&#xff1a; public class Arith { //加法运算 public static double add(dou…

Unity3D 场景与C# Control进行结合

杨航最近在自学Unity3D&#xff0c;打算使用这个时髦、流行、强大的游戏引擎开发一个三维业务展示系统&#xff0c;不过发现游戏的UI和业务系统的UI还是有一定的差别&#xff0c;很多的用户还是比较习惯WinForm或者WPF中的UI形式&#xff0c;于是在网上搜了一下WinForm和Unity3…

数据质量提升_合作提高数据质量

数据质量提升Author Vlad Rișcuția is joined for this article by co-authors Wayne Yim and Ayyappan Balasubramanian.作者 Vlad Rișcuția 和合著者 Wayne Yim 和 Ayyappan Balasubramanian 共同撰写了这篇文章 。 为什么要数据质量&#xff1f; (Why data quality?) …

黑魔法(method-swizzling)解决第三方库引发的问题

需求 最近做一个项目中&#xff0c;有个需求&#xff0c;所有网络请求&#xff0c;都不显示 NetworkActvityIndicator&#xff08;也就是状态栏里旋转的小圈圈&#xff09;. 解决过程1&#xff1a; 全局搜索 NetworkIndicator 关键字&#xff0c; 把所有涉及 NetworkIndicator …

Python 操作 MySQL 的5种方式(转)

Python 操作 MySQL 的5种方式 不管你是做数据分析&#xff0c;还是网络爬虫&#xff0c;Web 开发、亦或是机器学习&#xff0c;你都离不开要和数据库打交道&#xff0c;而 MySQL 又是最流行的一种数据库&#xff0c;这篇文章介绍 Python 操作 MySQL 的5种方式&#xff0c;你可以…

unity3d 人员控制代码

普通浏览复制代码private var walkSpeed : float 1.0;private var gravity 100.0;private var moveDirection : Vector3 Vector3.zero;private var charController : CharacterController;function Start(){charController GetComponent(CharacterController);animation.w…

删除wallet里面登机牌_登机牌丢失问题

删除wallet里面登机牌On a sold-out flight, 100 people line up to board the plane. The first passenger in the line has lost his boarding pass but was allowed in regardless. He takes a random seat. Each subsequent passenger takes their assigned seat if availa…

PHP 备份还原 MySql 数据库

原生 PHP 备份还原 MySql 数据库 支持 MySql&#xff0c;PDO 两种方式备份还原 php5.5 以上的版本建议开启pdo扩展&#xff0c;使用 pdo 备份还原数据 备份文件夹 db_backup、import/log 文件要有读写权限环境版本 本人测试环境 php:5.5.38 /5.6.27-nts/7.0.12-nts; mysql: 5.5…

Java™ 教程(Queue接口)

Queue接口 Queue是在处理之前保存元素的集合&#xff0c;除了基本的Collection操作外&#xff0c;队列还提供额外的插入、删除和检查操作&#xff0c;Queue接口如下。 public interface Queue<E> extends Collection<E> {E element();boolean offer(E e);E peek();…

字符串操作截取后面的字符串_对字符串的5个必知的熊猫操作

字符串操作截取后面的字符串We have to represent every bit of data in numerical values to be processed and analyzed by machine learning and deep learning models. However, strings do not usually come in a nice and clean format and require preprocessing to con…