加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

Java中的While循环语句用法

发布时间:2022-10-15 06:32:08 所属栏目:MsSql教程 来源:网络
导读: 这篇文章中,我们将学习如何在 Java 中使用 while 循环和 do while 循环。
在计算机编程中,循环用于重复一段代码。例如,如果您想要显示一条消息100次,那么您可以使用循环。这只是一个简

这篇文章中,我们将学习如何在 Java 中使用 while 循环和 do while 循环。

在计算机编程中,循环用于重复一段代码。例如,如果您想要显示一条消息100次,那么您可以使用循环。这只是一个简单的例子; 您可以通过循环实现更多。

在上一个教程中,您学习了 Java for 循环。在这里,您将学习 while 和 do... while 循环。

Java while 循环

Java while 循环用于运行特定的代码,直到满足某个条件为止。

While 循环的语法是:

while (testExpression) {
    // body of loop
}

来,给你:

A while loop evaluates the 循环计算textExpression 文本表达式 inside the parenthesis 在括号里().If the 如果textExpression 文本表达式 evaluates to 评价为true, the code inside the ,内部的代码while loop is executed. 执行循环The 这个textExpression 文本表达式 is evaluated again. 再次进行评估This process continues until the 这个过程一直持续到textExpression 文本表达式 is 是false.When the 当textExpression 文本表达式 evaluates to 评价为false, the loop stops. 循环停止 While 循环的流程图

Flowchart of Java while loop Java while 循环流程图:

while语句的语法格式_while循环用法_WHILE循环语法

例子1: 显示从1到5的数字

// Program to display numbers from 1 to 5
class Main {
  public static void main(String[] args) {
    // declare variables
    int i = 1, n = 5;
    // while loop from 1 to 5
    while(i <= n) {
      System.out.println(i);
      i++;
    }
  }
}

输出

1
2
3
4
5

下面是这个程序的工作原理。

WHILE循环语法_while语句的语法格式_while循环用法

例子二: 只限正数和

// Java program to find the sum of positive numbers
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
      
    int sum = 0;
    // create an object of Scanner class
    Scanner input = new Scanner(System.in);
    // take integer input from the user
    System.out.println("Enter a number");
    int number = input.nextInt();
	   
    // while loop continues 
    // until entered number is positive
    while (number >= 0) {
      // add only positive numbers
      sum += number;
      System.out.println("Enter a number");
      number = input.nextInt();
    }
	   
    System.out.println("Sum = " + sum);
    input.close();
  }
}

输出

Enter a number
25
Enter a number
9
Enter a number
5
Enter a number
-3
Sum = 39

在上面的程序中,我们使用 Scanner 类从用户那里获取输入。在这里,nextInt ()从用户获取整数输入。

While 循环继续,直到用户输入一个负数。在每次迭代过程中,用户输入的数字被添加到 sum 变量中。

当用户输入负数时,循环结束,最后显示总和。

2.Java do... while 循环

循环类似于 while 循环。但是,在检查测试表达式之前,执行 do... while 循环的主体一次。比如说:

while循环用法_WHILE循环语法_while语句的语法格式

让我们看看 do... while 循环的工作原理。

例子3: 显示从1到5的数字

// Java Program to display numbers from 1 to 5
import java.util.Scanner;
// Program to find the sum of natural numbers from 1 to 100.
class Main {
  public static void main(String[] args) {
    int i = 1, n = 5;
    // do...while loop from 1 to 5
    do {
      System.out.println(i);
      i++;
    } while(i <= n);
  }
}

输出

1
2
3
4
5

下面是这个程序的工作原理。

while语句的语法格式_while循环用法_WHILE循环语法

例子四: 正数和

// Java program to find the sum of positive numbers
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
      
    int sum = 0;
    int number = 0;
    // create an object of Scanner class
    Scanner input = new Scanner(System.in);
	   
    // do...while loop continues 
    // until entered number is positive
    do {
      // add only positive numbers
      sum += number;
      System.out.println("Enter a number");
      number = input.nextInt();
    } while(number >= 0); 
	   
    System.out.println("Sum = " + sum);
    input.close();
  }
}

输出1

Enter a number
25
Enter a number
9
Enter a number
5
Enter a number
-3
Sum = 39

在这里,用户输入一个正数,这个数字被加到 sum 变量中。这个过程一直持续到数字为负。当数字为负时,循环终止并显示和WHILE循环语法,而不加负数。

输出2

Enter a number
-8
Sum is 0

在这里,用户输入一个负数。测试条件将为 false,但循环中的代码执行一次。

无限 while 循环

如果循环的条件始终为真,则循环将无限次运行(直到内存满为止)。比如说,

// infinite while loop
while(true){
    // body of loop
}

下面是一个无限循环 do... while 的例子。

// infinite do...while loop
int count = 1;
do {
   // body of loop
} while(count == 1)

在上面的程序中,textExpression 总是为真,因此,循环体将运行无限次。

For 和 while 循环

当迭代次数已知时使用 For 循环,

for (let i = 1; i <=5; ++i) {
   // body of loop
}

While 和 do... while 循环通常用于迭代次数未知的情况,

while (condition) {
    // body of loop
}

想要写出一段基本的java代码程序,while循环语句的用法是必须要掌握的,这也是java零基础的新手小白首先要学习的基本内容之一,相信通过上面对退休金程序的介绍和解读,大家对while循环语句的用法更加理解了一些,希望大家平时可以多多练习使用while循环语句,提高自己的编程能力。

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!