迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

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

作者:迹忆客 最近更新:2023/09/28 浏览次数:

在本文中,我们将讨论在 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 部分用于显示菜单并接受用户输入,而条件部分取决于用户输入。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 Java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

发布时间:2023/12/17 浏览次数:187 分类:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

发布时间:2023/12/17 浏览次数:171 分类:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

发布时间:2023/12/17 浏览次数:192 分类:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:Java

这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

Java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:Java

了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便