java判断循环语句

判断语句

1、IF
public class TestAge{
public static void main(String args[]){
TestAge t = new TestAge();
t.age(75);
}
public void age(int age){
if (age< 0) {
System.out.println("不可能!");
} else if (age>250) {
System.out.println("是个妖怪!");
} else {
System.out.println("此人芳龄 " + age +
" 马马乎乎啦!");
}
}
}
2、SWITCH
public class Test{
      public static void main(String args[]){
int i = 1;
switch (i) {
      case 0:
System.out.println("zero");
break;
      case 1:
System.out.println("one");
//break;
        default:
System.out.println("default");
break;
}
       }
}
循环语句
1、循环语句分类
for 循环
while 循环
do/while 循环 
2、for 循环
public class ForLoop {
public static void main(String args[]){
         int result = 0;
         for(int i=1; i<=100; i++) {
 result += i;
         }
           System.out.println("result=" + result);
}
}
3、while 循环
public class WhileLoop {
       public static void main(String args[]){
        int result = 0;
int i=1;
while(i<=100) {
        result += i;
                         i++;
}
System.out.println("result=" + result);
        }
}
4、do/while 循环
public class WhileLoop {
       public static void main(String args[]){
        int result = 0,  i=1;
do{
        result += i;
                   i++;
}while(i<=100);
System.out.println("result=" + result);
      }
}
break and continue 语句
1、break只能用于switch语句和循环语句中。
continue 只能用于循环语句中。
2、Break示例
public class TestBreak{
public static void main(String args[]){
   for(int i = 0; i<10; i++){ 
    if(i==3)
     break;
    System.out.println(" i =" + i);
   }
   System.out.println("Game Over!");
}
}
3、Continue示例
public class ContinueTest {
       public static void main(String args[]){
    for (int i = 0; i < 100; i++) {
             if (i%10==0)
        continue;
                System.out.println(i);
        }
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xioateng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值