+ 1
(C#) Can I multiply a string by an integer?
I was attempting the âCheer Creatorâ challenge, which I had previously completed in Python, but when I tried to multiply the string âRa!â by the input (which is an integer), it gives an error because the string cannot be multiplied by an integer, which I didnât encounter as a problem in Python. I was wondering if there was a workaround to this, because I donât want to have to type a scenario for each number between 1 and 10. Thanks for any help.
2 Answers
+ 6
It won't be possible just like Python, but you can do this:
int mul = 4;
Console.WriteLine(
String.Concat(
Enumerable.Repeat("Ra!", mul)
)
);
But the task, I believe, is thought to be solved with loops. So, you do not need to type a scenario for each number 1 to 10, but only one loop and repeat as many times as you need the string printed using Console.Write, so that you don't have a line break in between.
+ 1
Oh ok. Thank you!