java - 使用扫描仪创建循环

标签 java loops

我正在努力让扫描仪接收用户输入的数字,然后打印 hello world 用户使用 while 循环输入该数字的次数。我为 x 创建了一个扫描仪,但我无法找到如何正确执行循环。

// import Scanner to take in number user imputs
import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args){
        // create a scanner class that takes in users number
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a whole number: " );
        // use x as the number the user entered
        int x = scan.nextInt();
        while ( ){
           System.out.println("Hello World!");
        }
    }
}

最佳答案

        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a whole number: " );
        // use x as the number the user entered
        int x = scan.nextInt();
        while (x > 0){
           System.out.println("Hello World!");
           x--;
        }

关于java - 使用扫描仪创建循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25728124/

相关文章:

java - 从 Android 应用程序使用 Java EE 应用程序服务器进行身份验证

java - 数据 未存储数据

php - 不重复地循环和排序结果

PHP 只返回第一个 MySQL 结果

C:即使条件仍然满足,循环也会弹出

java - 从 Eclipse 运行 web.xml 导致 HTTP 状态 404

Java:ArrayList、字符串操作和解析

java - primefaces 3.4,p :schedule showing events on wrong dates

javascript - 如何针对一系列特定条件优化 for 循环?

swift - 使用 for 循环时的预期声明