前言
本文是橙子出于兴趣爱好对Java官方教程的尝试翻译,几乎每日更新,感兴趣的朋友可以关注一下橙子;翻译过程中尽可能多的对一些关键词保留了英文原文,如果你想看最纯正的英文原版教材却又看不懂,可以试着来看一下橙子的翻译版啊,欢迎大家留言讨论,冲鸭!
更多相关文章点击阅读
Java官方教程目录2020最新版
Branching Statements 分支语句
The break Statement
break语句有2种形式,带标签的(labeled)和不带标签的。在之前对switch语句的讨论中看到了不带标签的形式。不带标签的break也可以用来终止for,while,或者do-while循环。
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循环。然后控制流(control flow)转移到for循环后面的语句。
输出为:
Found 12 at index 4
无标签的break语句终止最里面的switch,for,while或do-while语句,但是带标签的break终止外面的语句。下面程序与前面程序类似,但是使用嵌套的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语句终止带标签的语句,它不会将控制流转移到标签上。控制流转移到标记语句的下一句上。
The continue Statement
continue语句跳过for,while,do-while语句的当前迭代。未标记的形式将跳到最内层循环主体的末尾,并计算(evaluate)控制该循环的布尔表达式。
下面程序遍历( step through)一个字符串,统计字母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语句将跳过被标记外部循环的当前迭代。下面程序使用嵌套循环在另一个字符串中寻找子串。需要2个嵌套循环,一个循环访问子字符串,另一个循环访问要搜索的字符串。
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
The return Statement
最后一个分子语句是return语句。return语句退出当前方法,控制流返回到调用该方法的位置。return语句有2中形式,一种返回值,另一种不返回值。如果要返回值,只需将值(或计算值的表达式)放在return关键字之后。
return ++count;
返回值的数据类型必须与方法声明的返回值匹配。当方法声明为void时,请使用不带返回值的return形式。
return;
The Classes and Objects 课程将将涵盖有关编写方法的所有内容。