[/code]3.2.2 if-else
if-else语句或许是控制程序流程最基本的形式。其中的else是可选的,所以可按下述两种形式来使用if:
if(布尔表达式)
语句
或者
if(布尔表达式)
语句
else
语句
[code="java"]
static int test(int testval) {
int result = 0;
if(testval > target)
result = -1;
else if(testval < target)
result = +1;
else
result = 0; // match
return result;
}
[b]return[/b]
static int test2(int testval) {
if(testval > target)
return -1;
if(testval < target)
return +1;
return 0; // match
}
//不必加上else,因为方法在遇到return后便不再继续。
[b] 反复[/b]
public class WhileTest {
public static void main(String[] args) {
double r = 0;
while(r < 0.99d) {
r = Math.random();
System.out.println(r);
}
}
} ///:~
do-while的格式如下:
do
语句
while(布尔表达式)
while和do-while唯一的区别就是do-while肯定会至少执行一次;也就是说,至少会将其中的语句“过一遍”——即便表达式第一次便计算为false。而在while循环结构中,若条件第一次就为false,那么其中的语句根本不会执行。在实际应用中,while比do-while更常用一些。
[b] for[/b]
for循环在第一次反复之前要进行初始化。随后,它会进行条件测试,而且在每一次反复的时候,进行某种形式的“步进”(Stepping)。for循环的形式如下:
for(初始表达式; 布尔表达式; 步进)
语句
public class ListCharacters {
public static void main(String[] args) {
for( char c = 0; c < 128; c++)
if (c != 26 ) // ANSI Clear screen
System.out.println(
"value: " + (int)c +
" character: " + c);
}
} ///:~
可在for语句里定义多个变量,但它们必须具有同样的类型:
for(int i = 0, j = 1;
i < 10 && j != 11;
i++, j++)
/* body of for loop */;
public class CommaOperator {
public static void main(String[] args) {
for(int i = 1, j = i + 10; i < 5;
i++, j = i * 2) {
System.out.println("i= " + i + " j= " + j);
}
}
} ///:~
输出如下:
i= 1 j= 11
i= 2 j= 4
i= 3 j= 6
i= 4 j= 8
[b]中断和继续[/b]
在任何循环语句的主体部分,亦可用break和continue控制循环的流程。其中,break用于强行退出循环,不执行循环中剩余的语句。而continue则停止执行当前的反复,然后退回循环起始和,开始新的反复。
下面这个程序向大家展示了break和continue在for和while循环中的例子:
class BreakAndContinue
{
public static void main(String[] args)
{
for(int i=0;i<100;i++){
if(i==74) break;
if(i%9 != 0) continue;
System.out.println(i);
}
int i = 0;
while(true){
i++;
int j = i *27;
if(j==1269) break;
if(i%10 != 0) continue;
System.out.println(i);
}
}
}
public class LabeledWhile {
public static void main(String[] args) {
int i = 0;
outer:
while(true) {
prt("Outer while loop");
while(true) {
i++;
prt("i = " + i);
if(i == 1) {
prt("continue");
continue;
}
if(i == 3) {
prt("continue outer");
continue outer;
}
if(i == 5) {
prt("break");
break;
}
if(i == 7) {
prt("break outer");
break outer;
}
}
}
}
static void prt(String s) {
System.out.println(s);
}
}
Outer while loop
i = 1
continue
i = 2
i = 3
continue outer
Outer while loop
i = 4
i = 5
break
Outer while loop
i = 6
i = 7
break outer
[b]Switch[/b]
public class VowelsAndConsonants {
public static void main(String[] args) {
for(int i=0;i<100;i++){
char c = (char)(Math.random()*26 +'a');
System.out.print(c+": ");
switch(c){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("vowel");
break;
case 'y':
case 'w':
System.out.println("Sometimes a vowel");
break;
default :
System.out.println("consonant");
}
}
}
}
public class CastingNumbers {
public static void main(String[] args) {
double
above = 0.7,
below = 0.4;
System.out.println("above: " + above);
System.out.println("below: " + below);
System.out.println(
"(int)above: " + (int)above);
System.out.println(
"(int)below: " + (int)below);
System.out.println(
"(char)('a' + above): " +
(char)('a' + above));
System.out.println(
"(char)('a' + below): " +
(char)('a' + below));
}
} ///:~
输出结果如下:
above: 0.7
below: 0.4
(int)above: 0
(int)below: 0
(char)('a' + above): a
(char)('a' + below): a
//所以答案就是:将一个float或double值造型成整数值后,总是将小数部分“砍掉”,不作//任何进位处理。