java基础案例:for循环和while循环

/**
for和while的特点:
1,for和while可以互换;
2.格式上的不同,在使用上有点小区别;
如果需要通过变量来对循环进行控制,该变量只作为循环增量存在时,区别就体现出来了。  
*/

public class For_Demo5
{
public static void main(String[] args){

/*
int x = 1;
for(x=1;x<=100;x++)
{
if(x%6==0)
{
System.out.println("六的倍数有:"+x);
}
}


*/


/* //打印的一个三角形
for(int x=5;x>=1;x--)
{
for(int y=x;y<=5;y++)
{
System.out.print("*");
}
System.out.println();
}

*/


/*


for(int x=1;x<=5;x++)
{
for(int y=5;y>=x;y--)
{
System.out.print(y);
}
System.out.println();
}


*/


//这是打印的九九乘法表;


for(int x=1;x<=9;x++)
{
for(int y=1;y<=x;y++)
{
System.out.print(y+"*"+x+"="+y*x+"\t");
}
System.out.println("\n");
}




//这是打印的一个等腰三角形,可以给循环标号(取名字)列: abc:for(;;){} 这是为了方便break结束外循环的时候用
for(int x=1;x<=5;x++)
{
for(int y=1;y<x;y++)
{
System.out.print(" ");
}


for(int z=x;z<=5;z++)
{
System.out.print("* ");
}
System.out.println();
}


}

}


public class While_Demo4
{
public static void main(String[] args){
int x =10;
while(x>=1)
{
System.out.println("x="+x);
x--;
}


do
{
System.out.println("x=    "+x);
x++;
}
while (x<=10);
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值