简体   繁体   中英

loop based on user input java

I want to write a program that queries the user for a "number n" and then output the word repetition n times. For example the word is "Hello".

So far I've got this:

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

        Scanner scanner = new Scanner(System.in); 

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



    }

Which should end in this ->

Number: 3 Hello Hello Hell.

I don't know how to move forward.

I thank you for your time in advance!

You can use functional programming.

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

Here n is the number of times that string is to be printed and a variable is a string.

This code out print "Hallo" n times (n = numbers)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM