
Java 以其一致性和多功能性而闻名。 Java 提供了几种控制流主要方法。 Java 的词源结构缺乏控制流说明,如“goto”语句所示。在这一部分中,我们将了解为什么 Java 没有 goto 函数、它的一些选项,以及如何使用它们来实现类似的目标。
语法
首先,我们来看看 Java 的语言结构。 goto 解释使您能够根据名称自由交换代码部分。 Goto 在 C 和 C++ 中生成复杂的控制流,但代码通常不可读且毫无价值。
label: {
// Code section 1
if (condition) {
// Code section 2
if (anotherCondition) {
// Code section 3
break label;
} else {
// Code section 4
if (yetAnotherCondition) {
// Code section 5
if (finalCondition) {
// Code section 6
}
}
}
}
// Code section 7
}
语法解释
Java 的作者省略了 goto 表达,因为它会使代码变得混乱并且难以理解。他们青睐结构化控制流,以获得更清晰的代码和更少的错误。
算法
Java 中用于管理控制流的分步算法 -
立即学习“Java免费学习笔记(深入)”;
入口点- 程序的执行将从已选择的入口点开始,该入口点可以是主方法或另一个入口点。
按顺序执行 - 代码以连续的方式逐行运行,除非遇到控制流解释,在这种情况下执行会跳转到以下断言程序。
创建循环的语句循环语句(包括 for、while 和 do-while 语句)允许重复代码块,直到满足特定条件。
方法
即使 Java 没有 goto,开发人员也找到了构建类似功能的方法。
方法 1:标签和条件语句
说明
标签可以标记代码段,条件表达式可以根据条件控制执行。 Goto 缺乏控制力和可读性。
开发语言:java,支持数据库:Mysql 5,系统架构:J2EE,操作系统:linux/Windows1. 引言 32. 系统的结构 32.1 系统概述 33. 功能模块设计说明 43.1 商品管理 43.1.1 添加商品功能模块 53.1.2 商品列表功能模块 83.1.3 商品关联功能模块 93.
示例
public class GotoExample {
public static void main(String[] args) {
GotoExample program = new GotoExample();
program.execute();
}
private void execute() {
label: {
System.out.println("Executing Code Section 1");
// Code section 1
if (condition) {
System.out.println("Executing Code Section 2");
// Code section 2
if (anotherCondition) {
System.out.println("Executing Code Section 3");
// Code section 3
break label;
} else {
System.out.println("Executing Code Section 4");
// Code section 4
if (yetAnotherCondition) {
System.out.println("Executing Code Section 5");
// Code section 5
if (finalCondition) {
System.out.println("Executing Code Section 6");
// Code section 6
}
}
}
}
System.out.println("Executing Code Section 7");
// Code section 7
}
}
private boolean condition = true;
private boolean anotherCondition = true;
private boolean yetAnotherCondition = true;
private boolean finalCondition = true;
}
输出
Executing Code Section 1 Executing Code Section 2 Executing Code Section 3
说明
为了演示执行情况,我们将 System.out.println() 语句插入到每个代码部分。这使您可以跟踪执行情况并根据情况查看正在运行的内容。控制中心显示代码执行消息。
方法2 用方法封装
通过将代码封装在方法中来构造Java控制流。通过将软件分解为可管理的部分,方法调用让我们可以浏览它。
示例
public class GotoExample {
public static void main(String[] args) {
GotoExample program = new GotoExample();
program.execute();
}
private void execute() {
section1();
if (condition()) {
section2();
if (anotherCondition()) {
section3();
return;
} else {
section4();
if (yetAnotherCondition()) {
section5();
if (finalCondition()) {
section6();
}
}
}
}
section7();
}
private void section1() {
System.out.println("Executing Code Section 1");
// Code section 1
}
private void section2() {
System.out.println("Executing Code Section 2");
// Code section 2
}
private void section3() {
System.out.println("Executing Code Section 3");
// Code section 3
}
private void section4() {
System.out.println("Executing Code Section 4");
// Code section 4
}
private void section5() {
System.out.println("Executing Code Section 5");
// Code section 5
}
private void section6() {
System.out.println("Executing Code Section 6");
// Code section 6
}
private void section7() {
System.out.println("Executing Code Section 7");
// Code section 7
}
private boolean condition() {
// Define the condition logic
return true;
}
private boolean anotherCondition() {
// Define the anotherCondition logic
return true;
}
private boolean yetAnotherCondition() {
// Define the yetAnotherCondition logic
return true;
}
private boolean finalCondition() {
// Define the finalCondition logic
return true;
}
}
输出
Executing Code Section 1 Executing Code Section 2 Executing Code Section 3
说明
我添加了section3()、section4()、section5()、section6()和section7()方法。 Condition、anotherCondition、yetAnotherCondition 和 FinalCondition 被它们的过程所取代。这些方法适合您。
方法 3 状态机
状态机管理复杂的控制流。状态机以数学方式表示转换。状态机组织控制流。
示例
enum State {
STATE_1, STATE_2, STATE_3, STATE_4, STATE_5, STATE_6, STATE_7
}
public class GotoExample {
public static void main(String[] args) {
GotoExample program = new GotoExample();
program.execute();
}
private void execute() {
State currentState = State.STATE_1;
while (currentState != State.STATE_7) {
switch (currentState) {
case STATE_1:
section1();
currentState = State.STATE_2;
break;
case STATE_2:
if (condition()) {
section2();
currentState = State.STATE_3;
} else {
currentState = State.STATE_4;
}
break;
// Define other states and transitions
}
}
}
private void section1() {
System.out.println("Executing Code Section 1");
// Code section 1
}
private void section2() {
System.out.println("Executing Code Section 2");
// Code section 2
}
// Define other section methods and states
private boolean condition() {
// Define the condition logic
return true;
}
}
输出
Executing Code Section 1 Executing Code Section 2
说明
我们将条件推理添加到condition() 的技术规范中。如果可能,更改条件()的策略组。
方法4异常处理
Java 中的异常处理或带有已检查、未检查和错误的 Java 异常,以及 try、catch、 throw、 throws 和 finally 关键字的示例和用法。
示例
public class GotoExample {
public static void main(String[] args) {
try {
section1();
if (condition()) {
section2();
if (anotherCondition()) {
section3();
throw new GotoException();
} else {
section4();
if (yetAnotherCondition()) {
section5();
if (finalCondition()) {
section6();
throw new GotoException();
}
}
}
}
section7();
} catch (GotoException e) {
// Handle the exception to continue execution
// or perform any necessary operations
}
}
private static void section1() {
System.out.println("Executing Code Section 1");
// Code section 1
}
private static void section2() {
System.out.println("Executing Code Section 2");
// Code section 2
}
private static void section3() {
System.out.println("Executing Code Section 3");
// Code section 3
}
private static void section4() {
System.out.println("Executing Code Section 4");
// Code section 4
}
private static void section5() {
System.out.println("Executing Code Section 5");
// Code section 5
}
private static void section6() {
System.out.println("Executing Code Section 6");
// Code section 6
}
private static void section7() {
System.out.println("Executing Code Section 7");
// Code section 7
}
private static boolean condition() {
// Define the condition logic
return true;
}
private static boolean anotherCondition() {
// Define the anotherCondition logic
return true;
}
private static boolean yetAnotherCondition() {
// Define the yetAnotherCondition logic
return true;
}
private static boolean finalCondition() {
// Define the finalCondition logic
return true;
}
}
class GotoException extends Exception {
// Custom exception class to simulate the "goto" behavior
}
输出
Executing Code Section 1 Executing Code Section 2 Executing Code Section 3
说明
方法 4 复制 goto 声明来处理异常情况。为了“跳”到代码,我们发出 GotoException。尝试使用 GotoException 控制执行。
结论
即使没有 goto 表达,Java 的有序控制流方法也能工作。标记、有限的关节、状态机和有序编程范例可帮助工程师编写无错误、高效的代码。要创建值得信赖、易于理解的 Java 程序,您必须接受这些可能性并遵循最佳实践。










