您的位置:首页 > 职场人生

黑马程序员---------java判断循环语句

2015-05-15 16:39 330 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

判断语句

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);

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: