打印字符串'X'时间(无循环) [英] Print a String 'X' Times (No Loop)
本文介绍了打印字符串'X'时间(无循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以打印字符串'x'次?
Is it possible to print a string 'x' times?
例如,如果给定字符串
String q = "*";
假设用户输入数字'4'表示他们希望字符串重复的次数。
Let's say the user entered the number '4' for the amount of times that they wanted the string repeated.
该程序将打印:
****
推荐答案
你可以像这样使用递归
private void printStar(int n){
if(n > 0){
System.out.print("*");
printStar(n-1);
}
}
并且最初调用这样的方法 - printStar(4);
And call the method like this initially - printStar(4);
这篇关于打印字符串'X'时间(无循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文