带有用户输入的 Java Do-While 循环

Zeeshan Afridi 2023年1月30日 2022年7月18日
  1. 在 Java 中使用 do-while 循环
  2. do-while 循环语句
  3. 在 Java 中使用 do-while 循环获取用户输入
带有用户输入的 Java Do-While 循环

在本文中,我们将讨论在 Java 中使用 do while 循环。

在 Java 中使用 do-while 循环

do-while 循环类似于其他循环,如 java 中的 forwhile 循环。它还用于根据特定条件反复迭代。

do-while 循环的独特之处在于它的独特之处在于 do-while 循环至少执行一次循环体,然后执行循环的条件表达式,可以是 true。条件表达式必须是布尔表达式。

语法:

Do{
//body of the loop;
} while(Condition);

代码示例:

package codes;
public class Codes {
  public static void main(String[] args) {

    int count = 0;
    //Do-While loop
    do {
          System.out.println("This is the Do part of the loop");       // Body of the loop
    }
    // Conditional expression of the loop
    while (count > 1);
    System.out.println("The Conditional expression is false so the Loop is terminated in the first iteration ");
  }
}

输出:

This is the Do part of the loop
The Conditional expression is false so the Loop is terminated in the first iteration

do-while 循环语句

do-while 循环的工作非常简单。do-while 循环有两部分,一是主体部分,二是条件部分。

Do-while 循环如何工作

首先,无论条件表达式如何,主体都会执行一次,然后条件表达式会检查是否为 True。循环将继续执行;否则,循环将被终止。

代码示例:

package codes;
public class Codes {
  public static void main(String[] args) {

    int count = 0;
    //Do-While loop
    do {
          System.out.println("Count is "+ count);       // Body of the loop
    }
    // Conditional expression of the loop
    while (count++ < 9);
  }
}

输出:

Count is 0
Count is 1
Count is 2
Count is 3
Count is 4
Count is 5
Count is 6
Count is 7
Count is 8
Count is 9

在本例中,首先执行循环的 do 部分,然后检查条件,直到条件为 true。循环相应地进行了迭代,但随着条件变为,循环终止。

在 Java 中使用 do-while 循环获取用户输入

正如所讨论的,do-while 循环有时是 Java 编程语言的理想特性,因为有时你希望在循环终止之前执行循环体。就像显示菜单,要求玩游戏,甚至在循环的 do 部分中获取用户的输入,然后在循环的条件表达式中使用该输入。

do-while 循环中接受用户的输入是你将遇到的最有用的情况之一。

代码示例:

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String input;
        String buyAgain = null;

        do
        {
            System.out.println("********MENU********");
            System.out.println("Press 1 for Coke");
            System.out.println("Press 2 for Tea");
            System.out.println("Press 3 for Orange Juice");
            System.out.println("Press 4 for Coffee");

            input = scan.next();

            if(input.equals("1"))
            {
                System.out.println("Your Coke is ready, Please enjoy it");
            }
            else if(input.equals("2"))
            {
                System.out.println("Please take your Tea and enjoy");
            }
            else if(input.equals("3"))
            {
                System.out.println("Enjoy your Orange juice");
            }
             else if(input.equals("4"))
            {
                System.out.println("Here's your Coffe Please enjoy");
            }
            else
            {
                System.out.println("Invalid input\nPress 1 to view menu");
                buyAgain = scan.next();
             }
            System.out.println("Would you like something else\nPress 1 for yes and 0 for not now");
            buyAgain = scan.next();
        }
        while(!buyAgain.equals("0"));
    }
}

输出:

********MENU********
Press 1 for Coke
Press 2 for Tea
Press 3 for Orange Juice
Press 4 for Coffee
1
Your Coke is ready, Please enjoy it
Would you like something else
Press 1 for yes and 0 for not now
1
********MENU********
Press 1 for Coke
Press 2 for Tea
Press 3 for Orange Juice
Press 4 for Coffee
0
Invalid input
Press 1 to view menu
1
Would you like something else
Press 1 for yes and 0 for not now
0

在这段代码中,do 部分用于显示菜单并接受用户输入,而条件部分取决于用户输入。

Zeeshan Afridi avatar Zeeshan Afridi avatar

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

LinkedIn

相关文章 - Java Loop