判断语句
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);
}
}
}