while 循环结构
while (布尔表达式){
//循环内容
}
只要布尔表达式为true,循环会一直执行下去
大多数循环必须停止下来,需要一个表达是让循环停止下来结束循环
少数部分情况下需要一直循环执行,比如服务器的请求响应监听等
循环条件一直为true就会造成无限循环【死循环】,正常程序应避免死循环,会影响程序性能或造成程序卡死崩溃
思考:计算1+2+3+......100=?
package com.kuang.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出1-100
int i = 0;
while (i<100){
i++;
System.out.println(i);
}
}
}
package com.kuang.struct;
public class WhileDemon03 {
public static void main(String[] args) {
//计算1+2+3+...100=?
int i = 0;
int sum = 0;
while (i<=100){
sum =sum + i;
i++;
}
System.out.println(sum);
}
}
do ....while循环
对于while循环,如不满足条件则不进入循环,但有时候需要组满足条件也要至少执行一次。
do....while循环和while循环相似,不同的是 do...while至少会执行一次
do{
//代码结构
}while(布尔表达式);
package com.kuang.struct;
public class DoWhileDemo01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while (i<=100);
System.out.println(sum);
}
}
package com.kuang.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(初始化;布尔表达式;更新){
//代码语句
}
package com.kuang.struct;
public class ForDemo02 {
public static void main(String[] args) {
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i < 100; i++) {
if (i%2!=0){
oddSum+=i;
}else{
evenSum+=i;
}
}
System.out.println("100以内奇数何为: "+oddSum);
System.out.println("100以内偶数何为: "+evenSum);
}
}
ackage com.kuang.struct;
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 0; i <=1000; i++) {
if (i%5==0){
System.out.print(i+"\t");//把println 改为print
}
if (i%(5*3)==0){//每行3个
System.out.println(); //换行操作
//也可以写为 System.out.println("\n"); 换行操作
}
}
//println 输出换行
//print 输出不换行
}
}
package com.kuang.struct;
/*
1X1=1
1X1=2 2X2=4
1X1=3 2X2=6 3X3=9
1X1=4 2X2=8 3X3=12 4X4=16
1X1=5 2X2=10 3X3=15 4X4=20 5X5=25
1X1=6 2X2=12 3X3=18 4X4=24 5X5=30 6X6=36
1X1=7 2X2=14 3X3=21 4X4=28 5X5=35 6X6=42 7X7=49
1X1=8 2X2=16 3X3=24 4X4=32 5X5=40 6X6=48 7X7=56 8X8=64
1X1=9 2X2=18 3X3=27 4X4=36 5X5=45 6X6=54 7X7=63 8X8=72 9X9=81
*/
public class ForDemo04 {
public static void main(String[] args) {
/*
1.先打印第一列
2.把固定的1用一个循环包起来 并将1替换为新的参数j
3.去掉重复i<=j
4.调整样式(将println改为print
*/
for (int j = 1; j < 10; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i+"X"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}
}
for(声明语句:表达式){
//代码句子
}
声明语句:声明新的局部变量,该变量的类型必须和数组元素类型匹配,其作用域限定在循环语句块,其值与此时数组元素相等
表达式:表达式时要访问的数组名,或者返回值为数组的方法
package com.kuang.struct;
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};//定义一个数组
for (int i = 0;i<5;i++){
System.out.println(numbers[i]);
}
System.out.println("========================");
//遍历数组元素
for (int x:numbers){
System.out.println(x);
}
}
}
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i==10){
break;
}
}
}
}
public class ContinueDemo {
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);
}
/*
1.break在任何语句的主体部分,均可用break控制循环流程
2.break用于强行退出循环,不执行循环中剩余的语句(break语句在switch语句中使用)
3.continue 语句在循环语句体中,用于终止某次循环过程,
即跳过循环中尚未执行的语句,接着执行下一次是否执行循环的判定
*/
}
}
goto关键字(了解即可 不需要掌握)
public class LableDemo {
public static void main(String[] args) {
//打印101-150之间的质数
int count = 0;
outer:for (int i=101;i<150;i++ ){
for (int j = 2;j<i/2;j++){
if (i % j ==0){
continue outer;
}
}
System.out.print(i+" ");
}
}
}
public class TextDemo {
/*
*
***
*****
*******
*********
*/
public static void main(String[] args) {
//打印三角形 5行 不知道如何运行的要善用debug
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("");
}
}
}
原文:https://www.cnblogs.com/ariesmark/p/15082723.html