一个简单的Java循环字符串需要审查。

huangapple go评论82阅读模式
英文:

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 = &quot;&quot;;
while (x&gt;0)
{
   s = s + &quot;Oy&quot; + &quot; &quot;;
   x = x - 1;
}
return s;

For loop:-

String s = &quot;&quot;;
for(int i=0;i&lt;x;i++)
{
   s = s + &quot;Oy&quot; + &quot; &quot;;
}
return s;

huangapple
  • 本文由 发表于 2020年8月5日 15:23:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63260292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定