Day09-Java
Java流程控制
循环结构
-
while 循环
-
do…while循环
-
for循环
-
在Java5中引入了一种主要用于数组的增强型for循环
while循环
- whlie是最基本的循环,结构为
whlie(布尔表达式){
//循环内容
}
- 只要布尔表达式为ture,循环会一直执行下去
- 大多数情况会让循环停下来,需要一个让表达式失效的方式来结束循环
- 少部分情况需要循环一直执行比如服务器的请求响应监听
- 循环一直为ture会造成死循环,正常的业务编程中要避免死循环
package com.Kong.FlowControl.struct;
public class WhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while (i < 100) {
i++;
sum += i;
}
System.out.println("1到100的和为:"+sum);
}
}
do…while循环
- 对于while语句而言,如果不满足条件,则不能进入循环
- do…while循环和while循环相似,不同的是do…while循环至少会执行一次
do{
//代码语句
}while(布尔表达式);
- do…while与while的区别
- do…while先执行后判断,while先判断后执行
- do…while总是保证循环体至少会执行一次
package com.Kong.FlowControl.struct;
public class DoWhileDemo02 {
public static void main(String[] args) {
int a = 0;
while (a < 0) {
System.out.println(a);
a++;
}
System.out.println("==============");
do {
System.out.println(a);
a++;
} while (a < 0);
}
}
For循环
- for循环是支持迭代的一种通用结构,是最有效,最灵活的循环结构
- for循环执行的次数是在执行前就确定的,语法格式如下
for(初始化;布尔表达式;更新){
//代码语句
}
- 计算0-100之间奇数和偶数的和
package com.Kong.FlowControl.struct;
public class ForDemo02 {
public static void main(String[] args) {
int oddsum = 0;
int evensum = 0;
for (int odd = 1; odd <= 100; odd += 2) {
oddsum += odd;
}
for (int even = 0; even <= 100; even += 2) {
evensum += even;
}
/*
for(int i=0;i<=100;i++){
if(i%2==0){
evensum+=i;
}else{
oddsum+=i;
}
}
*/
System.out.println("0-100的奇数和为:" + oddsum);
System.out.println("0-100的偶数和为:" + evensum);
}
}
- 用while或for循环输出1-1000之间能被5整除的数,并且每行输出三个
-- For循环
package com.Kong.FlowControl.struct;
public class ForDemo03 {
public static void main(String[] args) {
int count = 0;
for (int i = 0; i <= 1000; i += 5) {
System.out.print(i + "\t");
count++;
if (count % 3 == 0) {
System.out.println();
//System.out.print("\n");
}
}
//println 输出完会换行
//print 输出完不会换行
}
}
=====================================
-- while循环
package com.Kong.FlowControl.struct;
public class ForDemo03 {
public static void main(String[] args) {
int count = 0;
int i = 0;
while (i <= 1000) {
System.out.print(i+"\t");
count++;
i+=5;
if (count % 3 == 0) {
System.out.println();
}
}
}
}
- 打印九九乘法表
package com.Kong.FlowControl.struct;
public class ForDemo04 {
public static void main(String[] args) {
int mul = 0;
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
mul = j * i;
System.out.print(j + "*" + i + "=" + mul + "\t");
}
System.out.println();
}
}
}
增强for循环
- Java增强for循环
for(声明语句:表达式)
{
//代码句子
}
- 声明语句:声明新的局部变量,该变量的类型必须和叔祖元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等
- 表达式:表达式是要访问的数组名,或者是返回值为数组的方法
package com.Kong.FlowControl.struct;
public class ForDemo05 {
public static void main(String[] args) {
int[] nums = {10,20,30,40,50};
for(int i=0;i<5;i++){
System.out.println(nums[i]);
}
System.out.println("==============");
//遍历数组的元素
for(int num : nums){
System.out.println(num);
}
}
}
break continue
- break在任何循环语句的主题部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句(break语句也可以用在switch语句中)
package com.Kong.FlowControl.struct;
public class BreakDemo01 {
public static void main(String[] args) {
int i=0;
while(i<10){
i++;
System.out.println(i);
if (i==3){
break;
}
}
System.out.println("123");
}
}
- continue语句用在循环语句体中,用于终止某次循环过程,跳过循环中尚未执行的语句,接着精选下一次是否执行循环的判定
package com.Kong.FlowControl.struct;
public class ContinueDemo01 {
public static void main(String[] args) {
int i = 0;
while(i<100){
i++;
if (i%10==0){
System.out.println();
continue;
}
System.out.print(i+"\t");
}
}
}
practice:打印一个三角形
package com.Kong.FlowControl.struct;
public class Practice {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("**");
}
for (int j = 1; j < i; j++) {
System.out.print("**");
}
System.out.println();
}
}
}
浙公网安备 33010602011771号