英文:
a simple loop string in java needs review
问题
所以我以前做过编程,但出于某种原因,我似乎无法弄清楚这个问题,而且我一直在过度思考解决方案。总结一下,第一个方法应该返回1个oy,第二个方法应该返回3个oy,第三个方法需要返回5个oy。我已经链接了项目的截屏,这不是为了学校或其他什么东西,只是一个朋友试图重新教我Java,然后最终学习全栈SQL。我主要处理Android。项目
英文:
so ive done coding before but for some reason i cant seem to figure this one out and i keep over thinking the solution. so to summarize, the first method is supposed to return 1 oys, the second method should return 3 oys, and the third methods needs to return 5 oys. i've linked an screen shot to the project, it isn't for school or anything like that, just a friend trying to reteach me java, and then eventually full stacks sql. i mainly have dealt with android. project
答案1
得分: 0
根据我的理解,您需要在 loopedOyMethod()
中构建一个字符串并返回它。您可以使用while循环或for循环来实现这个目标。
while循环:
String s = "";
while (x > 0)
{
s = s + "Oy" + " ";
x = x - 1;
}
return s;
for循环:
String s = "";
for(int i=0; i < x; i++)
{
s = s + "Oy" + " ";
}
return s;
英文:
Based on my understanding, you need to build a string in the loopedOyMethod()
and return it. You can do this by using a while loop or a for loop.
while loop:-
String s = "";
while (x>0)
{
s = s + "Oy" + " ";
x = x - 1;
}
return s;
For loop:-
String s = "";
for(int i=0;i<x;i++)
{
s = s + "Oy" + " ";
}
return s;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论