网站搜索

了解 Java 中的 Java 类、主要方法和循环控制 - 第 3 部分


在我们上一篇文章“Java 的工作和代码结构”中,我们重点介绍了 Java 的工作细节、Java 源文件、Java 类文件、类(公共/私有)、方法、语句、您的第一个 Java 程序、Java 的编译和运行程序。

在这个java学习系列指南中,我们将了解java类、主方法和循环控制是如何工作的,我们还将看到使用带有主方法和循环控制的Java类的基本代码。

Java 中的所有内容都在一个类中

Java中的一切都是对象,类是对象的蓝图。 Java中的每一段代码都放在类的花括号下。当您编译 Java 程序时,它会生成一个类文件。当您运行 Java 程序时,您实际上运行的不是程序文件,而是类。

当您在 Java 虚拟机 (JVM) 中运行程序时,它会加载所需的类,然后直接进入主 () 方法。程序继续运行直到主()方法的右大括号结束。程序在 main() 方法之后开始执行。一个类必须有一个主要的()方法。并不是所有的类(私有类)都需要一个 main () 方法。

main() 方法里面有什么?

main() 方法是魔法开始的地方。您可以要求 JVM 通过语句/指令和循环在 main() 方法中执行任何操作。

什么是循环?

循环是一个指令或多个按顺序的指令,不断重复直到达到条件。循环是编程语言的逻辑结构。循环逻辑结构通常用于执行一个过程,检查条件,执行一个过程,检查条件,……直到满足条件要求。

Java 中的循环

Java中存在三种不同的循环机制。

1. while 循环

Java中的while循环是一种控制结构,用于按照布尔表达式的定义重复执行某项任务一定次数,直到表达式测试结果为true。如果布尔表达式文本结果为 false,则 while 循环将被完全忽略,甚至不会执行一次。

while循环的语法:

while (boolean expression)
{
	statement/instructions
}

Java 中 while 循环的示例:

public class While_loop
{
    public static void main(String[] args)
    {
        int A = 100;
        while(A>0)
        {
            System.out.println("The Value of A = " +A);
            A=A-10;
        }
    }
}
样本输出
java While_loop 

The Value of A = 100
The Value of A = 90
The Value of A = 80
The Value of A = 70
The Value of A = 60
The Value of A = 50
The Value of A = 40
The Value of A = 30
The Value of A = 20
The Value of A = 10

While_loop 程序剖析

// Public Class While_loop
public class While_loop
{
    // main () Method
    public static void main(String[] args)
    {
        // declare an integer variable named 'A' and give it the value of 100
        int A = 100;
        // Keep looping as long as the value of A is greater than 0. 'A>0' here is the boolean                 
           expression
        while(A>0)
        {
	 // Statement
            System.out.println("The Value of A = " +A);
            // Post Decrement (by 10)
	 A=A-10;
        }
    }
}
2. do..while 循环

do...while 循环与 while 循环非常相似,不同之处在于它在 while 之前包含一个 do... 以确保循环至少执行一次。

while循环的语法:

do 
{
statement/instructions
}
while (boolean expression);

您可能会看到上面的语法清楚地显示了循环的 'do..' 部分在检查布尔表达式(无论它是 true 还是 false)之前执行。因此,无论布尔表达式的结果(真/假)是什么,循环都会执行。如果为真,它将执行直到条件满足。如果为 false 则执行一次。

Java 中的 do…while 循环示例:

public class do_while
{
    public static void main(String[] args)
    {
        int A=100;
        do
        {
            System.out.println("Value of A = " +A);
            A=A-10;
        }
        while (A>=50);
    }
}
样本输出
java do_while 

Value of A = 100
Value of A = 90
Value of A = 80
Value of A = 70
Value of A = 60
Value of A = 50

do_while 程序剖析:

// public class do_while
public class do_while
{
    // main () Method
    public static void main(String[] args)
    {
        // Declare a Integer Variable 'A' and assign it a value = 100
        int A=100;
        // do...while loop starts
        do
        {
            // execute the below statement without checking boolean expression condition if true 
               or false
            System.out.println("Value of A = " +A);
            // Post Decrement (by 10)
            A=A-10;
        }
        // Check condition. Loop the execute only till the value of Variable A is greater than or 
           equal to 50.
        while (A>=50);
    }
}
3.for循环

Java中的for_loop被广泛用于重复控制。它用于将任务迭代特定次数。 For循环用于控制执行某项任务需要执行循环次数。仅当您知道需要执行循环多少次时,for 循环才有用。

for循环的语法:

for (initialization; boolean-expression; update)
{
statement
}

An example of the for loop in Java

public class for_loop
{
    public static void main(String[] arge)
    {
        int A;
        for (A=100; A>=0; A=A-7)
        {
            System.out.println("Value of A = " +A);
        }
    }
}
样本输出
java for_loop 

Value of A = 100
Value of A = 93
Value of A = 86
Value of A = 79
Value of A = 72
Value of A = 65
Value of A = 58
Value of A = 51
Value of A = 44
Value of A = 37
Value of A = 30
Value of A = 23
Value of A = 16
Value of A = 9
Value of A = 2

for_loop 程序剖析:

// public class for_loop
public class for_loop
{
    // main () Method
    public static void main(String[] arge)
    {
        // Declare a Integer Variable A
        int A;
        // for loop starts. Here Initialization is A=100, boolean_expression is A>=0 and update is 
           A=A-7
        for (A=100; A>=0; A=A-7)
        {
            // Statement        
            System.out.println("Value of A = " +A);
        }
    }
}

Java 中循环的 Break 和Continue 关键字

1. Break 关键字

顾名思义,break 关键字用于立即停止整个循环。 break 关键字必须始终用在循环或 switch 语句内。一旦使用break中断循环; JVM 开始执行循环外的下一行代码。 Java中的break循环的一个例子是:

public class break
{
    public static void main(String[] args)
    {
        int A = 100;
        while(A>0)
        {
            System.out.println("The Value of A = " +A);
            A=A-10;
            if (A == 40)
            {
                break;
            }
        }
    }
}
样本输出
java break 

The Value of A = 100
The Value of A = 90
The Value of A = 80
The Value of A = 70
The Value of A = 60
The Value of A = 50
继续关键字

continue 关键字可以与 Java 中的任何循环一起使用。 continue 关键字要求循环立即跳转到下一次迭代。然而,for 循环和 while/do…while 循环对它的解释不同。

for循环中的Continue关键字跳转到下一个更新语句。

for 循环中的 continue 示例:

public class continue_for_loop
{
    public static void main(String[] arge)
    {
        int A;
        for (A=10; A>=0; A=A-1)
        {
	    if (A == 2)
		{
	        continue;
		}
            System.out.println("Value of A = " +A);
        }
    }
}
样本输出
java continue_for_loop

Value of A = 10
Value of A = 9
Value of A = 8
Value of A = 7
Value of A = 6
Value of A = 5
Value of A = 4
Value of A = 3
Value of A = 1
Value of A = 0

您是否注意到,它跳过了 A=2 的值。它通过转储到下一个更新语句来实现这一点。

2. while循环或do…while循环中的Continue关键字跳转到布尔表达式。

好吧,你可以自己做。这太容易了。只需按照上述步骤操作即可。

这就是我这边的全部内容。希望我在 Java 系列方面做得很好并对您有所帮助。保持联系以获取更多此类帖子。不要忘记在下面的评论中向我们提供您的宝贵反馈。