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