gpt4 book ai didi

java - 使用扫描仪创建循环

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:50 25 4
gpt4 key购买 nike

我正在努力让扫描仪接收用户输入的数字,然后打印 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com