我不知道如何重置循环(参见示例)

发布于 2024-10-06 23:41:24 字数 851 浏览 0 评论 0原文

我需要编写一个接受两个整数作为参数的方法,一个最小值和一个最大值。在第一行,我需要打印该范围内的所有数字(含)。在下一行中,我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

魂牵梦绕锁你心扉 2024-10-13 23:41:25

您应该考虑每行需要多少个值,然后确定这些值应该是什么。如果不给你解决方案,很难让它变得更清晰。

让我们知道你进展如何。

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.

一口甜 2024-10-13 23:41:25

彼得是对的,国际海事组织正在以正确的方式回答家庭作业问题。您知道每行需要多少个元素,因此您需要一个外循环来为您提供这么多元素,这将阻止您获得现在看到的级联行为。

此时,您需要考虑内部循环,您可能会发现使用模运算符 (%) 最简单。这将允许您进行迭代而不会超出您的目标。

你应该能够从那里弄清楚它,并且你自己弄清楚算法比从别人那里复制它要好得多,至少在这个开发水平上是这样。祝你好运!

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!

浅笑轻吟梦一曲 2024-10-13 23:41:25

考虑一种打印缺失数字的方法。答案如下,如果你想不出来可以查看。


这还应该打印缺失的部分:

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);         
      }  
      for (int k=0; k<i-min; k++){  
         System.out.print(min+k);         
      }  
      System.out.println(); 
   }  
}

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:

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);         
      }  
      for (int k=0; k<i-min; k++){  
         System.out.print(min+k);         
      }  
      System.out.println(); 
   }  
}
本宫微胖 2024-10-13 23:41:25

我没有运行这个,但它可能会起作用:

public void printSquare(int min, int max){

   int dif = max - min;
   for (int i=min; i<=max; i++){

      for (int j=i; j <= i+dif ; j++){
         int temp = j;
         if ( temp > max ) temp = temp  - max;
         System.out.print(temp);         
      }

   System.out.println();   
   }

}

I didn't run this one but it might work:

public void printSquare(int min, int max){

   int dif = max - min;
   for (int i=min; i<=max; i++){

      for (int j=i; j <= i+dif ; j++){
         int temp = j;
         if ( temp > max ) temp = temp  - max;
         System.out.print(temp);         
      }

   System.out.println();   
   }

}
冰雪梦之恋 2024-10-13 23:41:25

尝试像这样移动数组:

   static void Main(string[] args)
    {
        // this will work equally well with numbers letters or other types of characters
        int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        String a = "hello";
        for (int i = 0; i < nums.Length; i++)
        {
            int j = 5;
            int num = i;
            while (j-- > 0)
            {
                if (num >= nums.Length)
                {
                    num = 0;
                }

                // shift the loop
                Console.Write(nums[num++]);
            }
            Console.WriteLine();
        }
    }

try and just shift an array like so:

   static void Main(string[] args)
    {
        // this will work equally well with numbers letters or other types of characters
        int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        String a = "hello";
        for (int i = 0; i < nums.Length; i++)
        {
            int j = 5;
            int num = i;
            while (j-- > 0)
            {
                if (num >= nums.Length)
                {
                    num = 0;
                }

                // shift the loop
                Console.Write(nums[num++]);
            }
            Console.WriteLine();
        }
    }
白馒头 2024-10-13 23:41:25
public class Test1{
    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);       
            }
            for(int k= min; k<i; k++){
                System.out.print(k);     
            }
            //System.out.print(i-1);
            System.out.println();   
        }
    }
    public static void main(String[] args){
        Test1 t = new Test1();
        t.printSquare(1,5);
    }
}
public class Test1{
    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);       
            }
            for(int k= min; k<i; k++){
                System.out.print(k);     
            }
            //System.out.print(i-1);
            System.out.println();   
        }
    }
    public static void main(String[] args){
        Test1 t = new Test1();
        t.printSquare(1,5);
    }
}
破晓 2024-10-13 23:41:25

这是一个非常简单的实现。希望这有帮助!

   int n = max-min+1;
   for (int i=0 ; i<n; i++){
    for (int j=0; j<n; j++)
     cout<<min + (i+j)%n;
      cout<<"\n";
   }

输出:

min = 3 | max = 9 

    3456789
    4567893
    5678934
    6789345
    7893456
    8934567
    9345678

This is a very simple implementation. Hope this helps!

   int n = max-min+1;
   for (int i=0 ; i<n; i++){
    for (int j=0; j<n; j++)
     cout<<min + (i+j)%n;
      cout<<"\n";
   }

Output:

min = 3 | max = 9 

    3456789
    4567893
    5678934
    6789345
    7893456
    8934567
    9345678
笛声青案梦长安 2024-10-13 23:41:25

这是代码..

 for i = 0 to max-min
 for j = 0 to max-min
 print min + (i+j)%n

Here's the code..

 for i = 0 to max-min
 for j = 0 to max-min
 print min + (i+j)%n
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文