Java 教程是为 JDK 8 编写的。本页中描述的示例和实践未利用在后续版本中引入的改进。
break 语句break 语句有两种形式:带标签和无标签。你在前面关于 switch 语句的讨论中看到了无标签的形式。你也可以使用无标签的 break 终止for,while 或 do-while 循环,如在下面的 BreakDemo 程序中所示:
class BreakDemo {
public static void main(String[] args) {
int[] arrayOfInts =
{ 32, 87, 3, 589,
12, 1076, 2000,
8, 622, 127 };
int searchfor = 12;
int i;
boolean foundIt = false;
for (i = 0; i < arrayOfInts.length; i++) {
if (arrayOfInts[i] == searchfor) {
foundIt = true;
break;
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at index " + i);
} else {
System.out.println(searchfor + " not in the array");
}
}
}
该程序在数组中搜索数字 12。当找到该值时,以粗体显示的 break 语句会终止 for 循环。控制流然后转移到 for 循环之后的语句。这个程序的输出是:
Found 12 at index 4
无标签的 break 语句会终止最内层的 switch,for,while 或 do-while 语句,但带标签的 break 终止外部语句。以下程序 BreakWithLabelDemo 与前一个程序类似,但使用嵌套的 for 循环来搜索二维数组中的值。当找到该值时,带标签的 break 终止外部的 for 循环(标记为 "search"):
class BreakWithLabelDemo {
public static void main(String[] args) {
int[][] arrayOfInts = {
{ 32, 87, 3, 589 },
{ 12, 1076, 2000, 8 },
{ 622, 127, 77, 955 }
};
int searchfor = 12;
int i;
int j = 0;
boolean foundIt = false;
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at " + i + ", " + j);
} else {
System.out.println(searchfor + " not in the array");
}
}
}
这是该程序的输出。
Found 12 at 1, 0
break 语句终止带标签的语句;它不会将控制流转移到标签上。控制流被转移到紧随标签(已终止)语句之后的语句中。
continue 语句continue 语句跳过 for,while 或 do-while 循环的当前迭代。无标签形式跳到最内层循环体的末尾,并计算控制循环的 boolean 表达式。下面的程序 ContinueDemo,浏览 String ,计算字母 "p" 的出现次数。如果当前字符不是 p,continue 语句会跳过循环的其余部分并继续下一个字符。如果 是 "p",程序会增加字母计数。
class ContinueDemo {
public static void main(String[] args) {
String searchMe = "peter piper picked a " + "peck of pickled peppers";
int max = searchMe.length();
int numPs = 0;
for (int i = 0; i < max; i++) {
// interested only in p's
if (searchMe.charAt(i) != 'p')
continue;
// process p's
numPs++;
}
System.out.println("Found " + numPs + " p's in the string.");
}
}
这是这个程序的输出:
Found 9 p's in the string.
要更清楚地看到此效果,请尝试删除 continue 语句并重新编译。当你再次运行程序时,计数将是错误的,说它发现了 35 p 而不是 9。
带标签的 continue 语句跳过标有给定标签的外部循环的当前迭代。以下示例程序 ContinueWithLabelDemo 使用嵌套循环搜索另一个字符串中的子字符串。需要两个嵌套循环:一个循环遍历子字符串,一个迭代要搜索的字符串。以下程序 ContinueWithLabelDemo 使用标记形式继续跳过外循环中的迭代。
class ContinueWithLabelDemo {
public static void main(String[] args) {
String searchMe = "Look for a substring in me";
String substring = "sub";
boolean foundIt = false;
int max = searchMe.length() -
substring.length();
test:
for (int i = 0; i <= max; i++) {
int n = substring.length();
int j = i;
int k = 0;
while (n-- != 0) {
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue test;
}
}
foundIt = true;
break test;
}
System.out.println(foundIt ? "Found it" : "Didn't find it");
}
}
这是这个程序的输出。
Found it
return 语句最后一个分支语句是 return 语句。return 语句从当前方法中退出,并且控制流返回到调用方法的位置。return 语句有两种形式:一种返回值,另一种不返回。要返回值,只需在 return 关键字后面放置值(或计算值的表达式)即可。
return ++count;
返回值的数据类型必须与方法声明的返回值的类型匹配。当方法被声明为 void 时,使用 return 的形式不返回值。
return;
Classes and Objects 课程将涵盖你需要了解的有关编写方法的一切内容。