for(初始化语句;条件表达式;控制体语句){
循环体语句;
}
执行流程:初始化语句进行赋值,只执行一次
判断条件表达式是否成立,如果成立------执行循环体语句再执行控制体语句
继续判断条件表达式是否成立,如果成立------执行循环体语句再执行控制体语句
……
//重复打印”HelloWorld“十次
public class ForDemo {
public static void main(String[] args) {
for(int x = 1; x <= 10 ; x ++){
System.out.println("HelloWorld");
}
}
}
//求1~100整数的和
public class ForDemo02 {
public static void main(String[] args) {
int sum01 = 0;
for(int i = 1; i <=100; i++){
sum01 += i ;
}
System.out.println("1~100的和:"+sum01);
}
}
//求5的阶乘
public class ForDemo02 {
public static void main(String[] args) {
int jc = 1;//1!永远是1
for(int y = 1 ; y<=5; y++){
jc *= y;//jc = jc * y;
}
System.out.println("5的阶乘为:"+jc);
}
}
//所有的水仙花数
//水仙花数:每个位上的立方和等于这个数本身
//(153=1*1*1+5*5*5+3*3*3)
public class ForDemo03 {
public static void main(String[] args) {
for(int i=100;i<1000;i++){
int ge = i%10;
int shi = i/10%10;
int bai = i/100%10;
if (i == (ge*ge*ge+shi*shi*shi+bai*bai*bai)){
System.out.println("水仙花数有:"+i);
}
}
}
}
(二)while循环
格式:
while(条件表达式){
循环体语句;
控制体语句;
}
执行流程:初始化语句进行赋值;
判断条件表达式是否成立,如果成立,执行循环体语句,控制体语句对变量进行自增或者自减
继续判断条件表达式是否成立
……
如果条件表达式不成立,while循环结束
class WhileTest{
public static void main(String[] args){
//求出1-100之间的和
//定义一个最终结果变量
int sum = 0 ;
int x = 1;
while(x<=100){
sum += x ;
x ++ ;
}
System.out.println("1-100之间的和是"+sum) ;
//2)求出1-100之间的偶数和
System.out.println("----------------------------") ;
int sum2 = 0 ;
int y = 1 ;
while(y<=100){
if(y%2==0){
sum2 += y ;
}
y++ ;
}
System.out.println("1-100之间的偶数和是"+sum2) ;
System.out.println("----------------------------") ;
//求5的阶乘!
int jc = 1 ;
int m = 2;
while(m<=5){
jc *= m ;
m++ ;
}
System.out.println("5的阶乘是:"+jc) ;
System.out.println("----------------------------") ;
//求出所有的水仙花数
System.out.println("水仙花是以下:") ;
int j = 100 ; //三位数,从100开始
while(j<=999){
//确定每个位的数据本身
int ge = j % 10 ;
int shi = j /10 % 10 ;
int bai = j /10 /10 % 10 ;
//判断
if(j==(ge*ge*ge+shi*shi*shi+bai*bai*bai)){
//输出
System.out.println(j) ;
}
j ++ ;
}
System.out.println("----------------------------") ;
//统计所有的水仙花数有多少个
//定义统计变量
int count = 0 ;
int n = 100 ; //三位数,从100开始
while(n<=999){
//确定每个位的数据本身
int ge = n % 10 ;
int shi = n /10 % 10 ;
int bai = n /10 /10 % 10 ;
//判断
if(n==(ge*ge*ge+shi*shi*shi+bai*bai*bai)){
count ++ ;//统计变量++
//输出
System.out.println("第"+count+"次的水仙花是:"+n) ;
}
n++ ;
}
System.out.println("水仙花数共有:"+count+"个") ;
}
}
(三)do-while循环
格式:
初始化语句;
do{
循环体语句;
控制体语句;
}while(条件表达式);
执行流程:
初始化语句执行
执行条件表达式,如果条件表达式不成立,循环体至少执行一次,然后控制体语句以此判断;
public class DoWhileDemo {
public static void main(String[] args) {
int i = 7;//如果条件表达式不成立,循环体至少执行一次
do {
System.out.println("HelloWorld");
i++;
}while (i<=6);
}
}
二、循环的嵌套:for
格式
for(初始化语句1;条件表达式1;控制体语句1){
for(初始化语句2;条件表达式2;控制体语句2){
}
}
应用
class ForForDemo{
public static void main(String[] args){
//需要在控制台输出4行5列的*形
*****
*****
*****
*****
for(int x = 0 ; x <4; x++){//外层控制行数 :4行
//循环体:就是5列*
for(int y = 0; y <5 ; y ++){ //内层控制列数:5列*
System.out.print("*") ;
}
System.out.println() ;
}
}
}
class ForForTest{
public static void main(String[] args){
// 控制台输出
*
**
***
****
*****
for(int x = 0 ; x < 5 ;x++){ //5行
//变化的列数y<=x
for(int y = 0 ; y<=x ;y++){
System.out.print("*") ;
}
System.out.println() ;
}
}
}
class ForForTest2{
public static void main(String[] args){
//99乘法表:为了保证有数据,从1开始
for(int x = 1 ; x <= 9 ; x ++){//行数
//列数也在变化
for(int y = 1 ;y <=x ; y ++){
//Java中的转义字符
// x :X任意字符 t---"制表符"
System.out.print(y+"*"+x+"="+(y*x)+"t" ) ;
}
System.out.println() ;
}
}
}
三、跳转控制语句
(一)break
break:中断、结束循环
应用场景:
-
不能单独使用,报错
-
只能在两个场景中使用
-
switch语句自带了break关键字
-
循环结构语句中:for/while循环都可以用到
class BreakDemo{
public static void main(String[] args){
//break ;//在 switch 或 loop 外部中断
//写for循环
for(int x = 0 ; x <10 ; x ++){
if(x == 3){
break ;//结束,中断
}
System.out.println(x) ;//结果:0,1,2
}
}
}
(二)continue
continue:属于跳转控制语句中的一种,一般都在循环语句中使用的,表示结束当前循环,立即进入下一次循环了…
class ContinueDemo{
public static void main(String[] args){
//for循环
for(int x =1 ; x <= 10 ; x ++){
//加入条件
if(x ==3 ){
//break ; //结束
continue ;//结束当前循环,立即进入下一次循环了
}
System.out.println(x) ; // break:1,2
//contine:1,2,4,5,6,7,8,9,10
}
}
}
(三)return
return:返回结果(结束方法去使用,很少单独使用)
实际开发中,只要看到return 关键字,肯定是当前方法中有具体返回值类型的时候去使用!



