Java while 循环示例
循环是 Java 编程功能,仅当给定条件为真时,才以重复方式运行代码的特定部分。
在 Java 中,while 循环是一种迭代控制流模型,其中条件重复运行,直到编码的布尔条件变为真。它是一个重复的 if 条件,可用于执行一些语句块。当进程中的布尔数迭代不固定时,就需要在代码中使用 while 循环。
Java 环境中 While 循环的工作原理是什么?
在 Java 环境中,while 循环首先检查给定条件。如果进程返回 TRUE,则代码将继续运行。否则,特定条件提供或返回 FALSE 值,整个进程将在该点终止。并且 while 循环可能根本不执行。
以下是 while 循环如何执行特定任务的过程 -
在 parenthesis() 方法中评估 textExpression。
如果 textExpression 给出的评估为 TRUE,则 while 循环将执行。
再次扫描 textExpression。
整个方法将运行,直到 textExpression 变为 FALSE。
当我们得到 FALSE 评估时。整个循环将终止。
通过使用流程图模型算法,循环项变为运动中的循环。
工作循环有一些属性。
标志初始化。
文本表达式。
一个或多个操作。
更新标志。
以下是 Java 中 while 循环的工作流程模型 -
测试表达式 - 文本是一个表达式,我们必须通过它来测试条件是否满足逻辑。如果条件评估为真,则我们将使用循环执行整个源代码并更新表达式。否则,进程将退出 while 循环。示例:i <= 10
更新表达式 − 执行循环后,此特定表达式将使用数据中存在的某个值的增量/减量来执行迭代过程。示例:i++;
以下是 while 循环在进程中执行自身的过程 -
控制权属于编码的 while 循环。
流程跳转到提到的条件。
测试条件是真还是假。
如果条件为真,则流程再次进入代码主体。
如果条件为假,则流程超出循环。
循环主体内此处提到的语句将被执行。
更新发生在进程执行之后。
控制权再次流回到步骤 2。
while 循环已结束这里。
流程已经超出下一个给定条件的范围。
算法
步骤 1 - 开始。
步骤 2 - 构建一个实例扫描器类。
步骤 3 - 数字声明。
步骤 4 - 要求初始化该特定数字。
步骤 5 - 要打印乘法表,请使用 while 循环。
步骤 6 - 显示结果。
步骤 7 - 终止该过程。
While 循环的语法
while (condition){
Increment / decrement statement
}
Or;
while (test_expression){
// statements
update_expression;
}
在 while 循环中测试表达式。我们将找到该特定数字的乘法表。要替换该乘法表,用户可以在代码中应用 while 循环。
方法
方法 1 - while 循环的一般说明。
示例 1:打印一定次数的句子
在此 Java 构建的代码中,我们可以使用 while 循环多次打印输出。此处的时间复杂度为 O(1),辅助空间与之前相同
public class whileLoopDemo {
public static void main(String args[]){
int i = 1;
while (i < 10) {
System.out.println("Hello Coder");
i++;
}
}
}
输出
Hello Coder Hello Coder Hello Coder Hello Coder Hello Coder Hello Coder Hello Coder Hello Coder Hello Coder
示例 2:一些数字的求和
在此 Java 构建的代码中,我们使用 while 循环打印多个数字求和的输出。此处的时间复杂度为 O(1),辅助空间保持不变。
public class whileLoopexampletp {
public static void main(String args[]){
int x = 1, sum = 0;
while (x <= 20) {
sum = sum + x;
x++;
}
System.out.println("Summation of the numbers: " + sum);
}
}
输出
Summation of the numbers: 210
示例 3:打印无限循环
在此 Java 构建代码中,我们使用 while 循环来运行无限 while 循环。此处的时间复杂度为 O(1),辅助空间保持不变。
public class Whiledotp {
public static void main(String[] args) {
while(true){
System.out.println("infinitive while loop");
}
}
}
输出
infinitive while loop infinitive while loop infinitive while loop infinitive while loop infinitive while loop infinitive while loop continue…………
示例 4:数字的乘法表
在此代码中,我们学习了如何获取特定数字的乘法表。当我们输入特定数字作为输入时,代码由 while 循环构建。它会将整个乘法表打印为输出。
import java.util.*;
public class Main{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
int n=sc.nextInt();
int i=1;
System.out.println("The multiplication table of the number"+n+" is: ");
while(i<=10){
System.out.println(n+" * "+i+" = "+ (n*i));
i++;
}
}
}
输出
Enter a number: 8 The multiplication table of the number8 is: 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 8 * 10 = 80
结论
While 循环是 Java 环境中的迭代控制和条件检查过程。在本文中,我们学习了很多 while 循环。我们了解了如何使用条件及其独特行为的好处。以及我们如何使用哪个 while 循环。今天我们使用 Java 环境构建了一些代码。
数字的乘法表相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

