分类: Java
2008-08-03 23:58:18
1.1.1. 代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javatutorials;
/**
*
* @author wanpor
*/
public class Condition {
static int left = 4;
static int right = 8;
public static void main(String[] args){
ifelse();
switchdefault();
}
static void ifelse(){
//if语句
if(left > right){
System.out.println("left");
}
//if...else...语句
if(left > right){
System.out.println("left");
}else if (left < right){
System.out.println("rigth");
}else if (left == right){
System.out.println("left & right");
}
}
//switch语句
static void switchdefault(){
switch(left - right){
case 0:System.out.println("left & right");break;
case 1:System.out.println("left");break;
case 2:System.out.println("right");break;
default:System.out.println("default");
}
}
}
1.1.2. 说明
1. 单条件判断if(a > b) System.out.println(a);
2. 双条件判断if(a > b) System.out.println(a); else System.out.println(b);
3. 多条件判断,在else分支中使用嵌套;或使用switch控制
1.2.1. 代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javatutorials;
/**
*
* @author wanpor
*/
public class
static int left = 4;
static int right = 8;
public static void main(String[] args){
dowhile();
dofor();
}
//while/do...while语句
static void dowhile(){
do{
System.out.println(left--);
}while(left > 0);
while(left < 4){
System.out.println(left++);
}
}
//for语句
static void dofor(){
for(int i = 0; i < left;i++){
System.out.println(left - i);
}
for(int i = left;i > 0; i--){
System.out.println(left - i);
}
}
}
1.2.2. 说明
do…whle先执行再进行判断,程序至少执行一次;
while先判断再执行,循环体可能不执行;
for执行特定次数的重复操作;
1.3.1. 代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javatutorials;
/**
*
* @author wanpor
*/
public class Return {
static int left = 4;
static int right = 8;
public static void main(String[] args){
}
//只打印小于2的值
static void breakfor(){
for(int i = 0; i < left;i++){
if(i == 2)
break;
System.out.println(i);
}
}
//只打印小于2的值
static void returnfor(){
for(int i = 0; i < left;i++){
if(i == 2)
return;
System.out.println(i);
}
}
//大于不等于2的值
static void continuefor(){
for(int i = 0; i < left;i++){
if(i == 2)
continue;
System.out.println(i);
}
}
}
1.3.2. 说明
break:中断整个循环
continue:中断一下循环
return:直接从函数返回
|