Simple way to repeat a string

后端 未结 30 3487
清酒与你
清酒与你 2020-11-21 06:55

I\'m looking for a simple commons method or operator that allows me to repeat some string n times. I know I could write this using a for loop, but I wish to avoid f

30条回答
  •  生来不讨喜
    2020-11-21 06:58

    Try this out:

    public static char[] myABCs = {'a', 'b', 'c'};
    public static int numInput;
    static Scanner in = new Scanner(System.in);
    
    public static void main(String[] args) {
        System.out.print("Enter Number of Times to repeat: ");
        numInput = in.nextInt();
        repeatArray(numInput);
    }
    
    public static int repeatArray(int y) {
        for (int a = 0; a < y; a++) {
            for (int b = 0; b < myABCs.length; b++) {
                System.out.print(myABCs[b]);                
            }
            System.out.print(" ");
        }
        return y;
    }
    

提交回复
热议问题