简体   繁体   English

我无法弄清楚如何重置循环(参见示例)

[英]I can't figure out how to reset a loop (see example)

I need to write a method that accepts two ints as arguments, a min and a max. 我需要编写一个接受两个int作为参数的方法,一个min和一个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. 在下一行,我从min + 1开始,打印所有数字到最大值,然后返回到范围的前面并打印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. 下一行我从min + 2开始,依此类推,直到我从范围内的每个数字开始重复此操作。很难解释,这里有两个例子:假设我将1和5作为min和max参数传递。 I want the method to print this: 我想要方法打印这个:

12345
23451
34512
45123
51234

Or if 3 and 9 were passed, I would expect this: 或者如果3和9通过,我会期望这样:

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();   
   }
}

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

Here's the code.. 这是代码..

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

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. 彼得是对的,IMO正在以正确的方式回答家庭作业问题。 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! 祝好运!

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);
    }
}

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(); 
   }  
}

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();   
   }

}

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();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法弄清楚如何重置一个int,因此我可以正确使用循环来制作Pig游戏 - Can't figure out how to reset an int so I can use my loop properly making the game of Pig 无法弄清楚For Loop - Can't figure out For Loop JavaFX-线程挂起,我不知道如何在UI线程外运行循环 - JavaFX - Thread hangs, and I can't figure out how to run my loop outside the UI thread 我不知道如何在while循环中放置此if语句 - I can't figure out how to put this if statement in my while loop 我不知道如何使用带有 if 语句字符串比较的 while 循环 - i can't figure out how to use a while loop with an if statement string comparison (Java)我无法弄清楚如何使用所需的计数器控制循环来填充数组 - (Java) I can't figure out how to populate an array with a counter controlled loop which is required 当这个人输入错误的输入时,我不知道如何重新启动循环 - I can't figure out how to restart a loop, when the person enters in a wrong input Android Java:无法弄清楚如何循环我的VideoView - Android Java: Can't figure out how to loop my VideoView 无法找出for循环中的错误 - can't figure out mistake in a for loop 我不知道如何安排这些按钮,如链接所示 - I can't figure out how to arrange these buttons as seen in the link
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM