我不知道如何重置循环(参见示例)
我需要编写一个接受两个整数作为参数的方法,一个最小值和一个最大值。在第一行,我需要打印该范围内的所有数字(含)。在下一行中,我从 min+1 开始,打印直到 max 的所有数字,然后返回到范围的前面并打印 min。下一行我从 min+2 开始,依此类推,直到我从范围内的每个数字开始重复此操作。很难解释,这里有两个例子:假设我传递 1 和 5 作为 min 和 max 参数。我想要打印这个的方法:
12345
23451
34512
45123
51234
或者如果通过了 3 和 9,我会期望这样:
3456789
4567893
5678934
6789345
7893456
8934567
9345678
我已经尝试了各种方法,我确信有一种我没有意识到的简单方法可以做到这一点。我应该在没有数组或数组列表的情况下执行此操作。我认为我有一个良好的工作基础,但我就是不知道接下来该去哪里。我的基本代码打印出这个:
12345
2345
345
45
5
还有这个:
3456789
456789
56789
6789
789
89
9
我被难住了。这是我的代码:
public void printSquare(int min, int max){
for (int i=min; i<=max; i++){
for (int j=i; j<=max; j++){
System.out.print(j);
}
System.out.println();
}
}
I need to write a method that accepts two ints as arguments, a min and a max. On the first line i need to print all numbers in that range (inclusive). On the next line I start with min+1, print all numbers up to max, and then go back to the front of the range and print min. Next line I start with min+2, and so on until I have repeated this starting with each number in the range.Very hard to explain, here's two examples: Say I pass 1 and 5 as the min and max arguments. I want the method to print this:
12345
23451
34512
45123
51234
Or if 3 and 9 were passed, I would expect this:
3456789
4567893
5678934
6789345
7893456
8934567
9345678
I've tried all kinds of things, I'm sure there is an easy way to do this that I am not realizing. I'm supposed to do this without arrays or arrayLists. I think I have a good base to work with, but I just can't figure out where to go from here. My base code prints this:
12345
2345
345
45
5
And this:
3456789
456789
56789
6789
789
89
9
I'm stumped. Here's my code:
public void printSquare(int min, int max){
for (int i=min; i<=max; i++){
for (int j=i; j<=max; j++){
System.out.print(j);
}
System.out.println();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您应该考虑每行需要多少个值,然后确定这些值应该是什么。如果不给你解决方案,很难让它变得更清晰。
让我们知道你进展如何。
You should think about how many values you want on each row, and then determine what those values should be. Its hard to make it any clearer without giving you the solution.
Let us know how you go.
彼得是对的,国际海事组织正在以正确的方式回答家庭作业问题。您知道每行需要多少个元素,因此您需要一个外循环来为您提供这么多元素,这将阻止您获得现在看到的级联行为。
此时,您需要考虑内部循环,您可能会发现使用模运算符 (%) 最简单。这将允许您进行迭代而不会超出您的目标。
你应该能够从那里弄清楚它,并且你自己弄清楚算法比从别人那里复制它要好得多,至少在这个开发水平上是这样。祝你好运!
Peter is right, and IMO is answering a homework question in the right manner. You know how many elements you want on each line, so you need an outer loop that gives you that many elements, this will stop you from getting the cascading behavior you're seeing now.
At that point you need to think about your inner loop(s), and you'll probably find this easiest using the modulus operator (%). This will allow you to iterate without ever going over your target.
You should be able to figure it out from there, and you're much better off figuring out the algorithm yourself than copying it from someone else, at least at this level of development. Good Luck!
考虑一种打印缺失数字的方法。答案如下,如果你想不出来可以查看。
这还应该打印缺失的部分:
Think about a way to print the missing numbers. The answer is below, if you cannot come up with it you can check it.
This should also print the missing parts:
我没有运行这个,但它可能会起作用:
I didn't run this one but it might work:
尝试像这样移动数组:
try and just shift an array like so:
这是一个非常简单的实现。希望这有帮助!
输出:
This is a very simple implementation. Hope this helps!
Output:
这是代码..
Here's the code..