基于用户输入的循环

问题描述 投票:-2回答:2

我想写一个程序,查询用户的 "数字n",然后输出这个词重复n次.比如这个词是 "你好"。

到目前为止,我已经得到了这个。

public static void main(String[] args) throws IOException {

        Scanner scanner = new Scanner(System.in); 

        System.out.println("Zahl 1:");
        int number = scanner.nextInt();



    }

它的结尾应该是这样的 ->

数字: 3HelloHello Hell.

我不知道该如何继续前进。

先谢谢你的时间!

java user-input
2个回答
0
投票

这段代码输出打印 "Hallo "n次(n = numbers)

 String s = "Hello";
 for (int i = 0; i < number; i++){
      System.out.println(s);
 }

1
投票

你可以使用函数式编程。

IntStream.range(0, n)
           .forEach(System.out.println(variable));

这里n是该字符串的打印次数,变量是一个字符串。

© www.soinside.com 2019 - 2024. All rights reserved.