JAVA学习笔记(四) - 循环语句
2015-03-21 08:44
405 查看
while循环
do..while循环
for循环
题目:打印输出1~100之间偶数的和
题目:打印输出1000~2050年中所有的闰年
break语句
continue语句
嵌套循环
要求,使用*打印输出一个直角三角形
打印等腰三角形
/* * while循环 * 循环变量,可以控制循环次数。 */ public class Test { public static void main(String[] args) { int count = 5; while(count > 0) { System.out.println("Hello World " + count--); } System.out.println(count); System.out.println("hahahhaa"); } }
/* * while循环 * 实现1~100之和 */ public class Test { public static void main(String[] args) { //i表示循环变量,此时i不仅可以控制循环次数,i还可以表示具体数值。 int i = 1; int sum = 0;//用于存放结果 while(i <= 100) { sum += i;//sum = sum + i; i++;//循环变量自增 } System.out.println(sum); } }
do..while循环
/* * do..while循环 * while循环区别? */ public class Test { public static void main(String[] args) { //循环变量,主要用于控制循环。 int count1 = 0; do { System.out.println("Hello World"); }while(count1 > 0); //do..while循环,实现1~100之和 int i = 1; int sum = 0; do { sum += i;//sum = sum + i; i++; }while(i <= 100); System.out.println(sum); } }
for循环
/* * for循环 */ public class Test { public static void main(String[] args) { for(int i = 0; i < 5; i++) { System.out.println("Hello World"); } /* * for语句实现1~100之和 */ int sum = 0; for(int i = 1; i <= 100; i++) { sum += i; } System.out.println(sum); // int i = 0; // for(System.out.println("表达式1"); i++ < 2; System.out.println("表达式3")) // { // System.out.println("循环体"); // } } }
/* * for注意点 */ public class Test { public static void main(String[] args) { // 表达式1,2, 3可以有多个语句 for (int x = 0, y = 10; x < 5; x++, y++) { } // for中三个表达式都可有可无 int i = 1; int sum = 0; for (; i <= 100;) { sum += i; i++; } System.out.println(sum); // 第二个表达式如果没有,表示永久为true for (;;) { System.out.println("Hello World"); } } }
题目:打印输出1~100之间偶数的和
/* * 题目:打印输出1~100之间偶数的和 */ public class Test { public static void main(String[] args) { //while int i1 = 1; int sum1 = 0; while(i1 <= 100) { if(i1 % 2 == 0) { sum1 += i1; } i1++; } System.out.println(sum1); //for int sum2 = 0; for(int i2 = 1; i2 <= 100; i2++) { if(i2 % 2 == 0) { sum2 += i2; } } System.out.println(sum2); } }
题目:打印输出1000~2050年中所有的闰年
/* * 题目:打印输出1000~2050年中所有的闰年 */ public class Test { public static void main(String[] args) { //while int year = 1000; while(year <= 2050) { if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { System.out.println(year); } year++; } //for for(int y = 1000; y <= 2050; y++) { if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { System.out.println(year); } } } }
break语句
/* * break * 在循环中,一旦运行到break语句,则终止循环。 */ public class Test { public static void main(String[] args) { //break for(int i = 100; i > 0; i--) { if(i % 7 == 0) { System.out.println(i); break; } } int count = 10; while(true) { if(count % 7 == 0) { break; } System.out.println(count--); } System.out.println("for外代码"); } }
continue语句
/* * continue * 终止本次循环,进入下次循环。 */ public class Test { public static void main(String[] args) { int i = 0; while(i++ < 5) { if(i % 3 == 0) { continue; } System.out.println(i); } //通过continue,实现1~100之间偶数之和 int sum = 0; int a = 1; while(a <= 100) { if(a % 2 != 0) { a++; continue; } sum += a; a++; } System.out.println(sum); } }
嵌套循环
/* * 嵌套循环 */ public class Test { public static void main(String[] args) { for(char c = 'A'; c <= 'D'; c++) { for(int i = 0; i <= 3; i++) { System.out.print(c + "" + i + "\t"); } System.out.println();//换行 } } }
要求,使用*打印输出一个直角三角形
/* * 要求,使用*打印输出一个直角三角形 * */ public class Test { /* * ** *** **** ***** ****** ******* */ public static void main(String[] args) { //打印七行 for(int row = 0; row < 7; row++) { for(int col = 0; col <= row; col++) { System.out.print("*"); } System.out.println(); } } }
打印等腰三角形
/* 5 - row * 2*row - 1 *** ***** ******* ********* */ public class Test { public static void main(String[] args) { for(int row = 1; row <= 5; row++) { //打印空格 for(int spa = 1; spa <= 5 - row; spa++) { System.out.print(" "); } //打印* for(int col = 1; col <= (2 * row - 1); col++) { System.out.print("*"); } System.out.println(); } } }
相关文章推荐
- Java学习笔记03--判断与循环语句
- java学习笔记(三) -- 判断 循环语句
- Java学习笔记八:Java的流程控制语句之循环语句
- java学习笔记---循环与选择语句
- java学习笔记03--判断与循环语句
- java学习笔记03--判断与循环语句
- Java基础学习笔记 -- 8(循环语句)
- Java学习笔记---4.Java的分支循环语句
- Java学习笔记九:Java的循环跳转语句
- 【Java学习笔记之十】Java中循环语句foreach使用总结及foreach写法失效的问题
- java学习笔记(七)流程控制之循环语句
- [Java学习笔记] 猜猜看(2) ~ 中断循环语句的一种方法(能够中断所有进行中的循环)
- 黑马程序员_java基础学习笔记02_选择结构和循环语句语句
- 黑马程序员-java学习笔记-循环语句
- java学习笔记03--判断与循环语句
- Java基础学习笔记(一)环境变量配置,数据类型,运算符,控制语句,循环语句
- Java学习笔记001——学前准备、数据类型、运算符、流程控制语句
- Python学习笔记:条件、循环和其他语句
- PHP学习笔记5-PHP循环语句的介绍与应用
- Python基础教程学习笔记----第五章 条件、循环和其他语句