生成10个数字并将第一个数字移动到最后10次

时间:2016-12-28 05:00:11

标签: android

我有生成1到10个数字的代码:

for (int i = 0; i <=10; i++)
{
     for (int j = 1; j <=10; j++)
     {
         int respones = i;
         int respones1 = j;

         if (respones1 > respones)
         {
              text.append(String.valueOf(respones1));
         }
     }
}

我得到了这个结果:

12345678910
2345678910
345678910
45678910
5678910
678910
78910
8910
910
10

但是,我想要这个结果:

12345678910
23456789101
34567891012
45678910123
56789101234
67891012345
78910123456
89101234567
91012345678
10123456789

如何获取我的代码,以便将第一个数字移动到字符串的末尾?

9 个答案:

答案 0 :(得分:8)

试试这个:

for (int i = 0; i < 10; i++) {
    for (int j = i + 1; j <= i + 10; j++) {
        int respones = i;
        int respones1 = j;
        if (respones1 > respones) {
            text.append(String.valueOf(respones1 > 10 ? respones1 % 10 : respones1));
        }
    }
}

答案 1 :(得分:3)

我认为解决这个问题的最佳方法是使用deque /队列,因为它可以在两个操作中实现旋转序列中的数字。

ArrayDeque<int> deque = new ArrayDeque<int>(10); // Create a queue 
for(int i = 1; i <= 10; ++i) // Fill queue with [1..10]
{
    deque.addLast(i);
}

StringBuilder builder = new StringBuilder(); // A StringBuilder will be more efficient than string concatenation
for(int i = 1; i <= 10; ++i)
{
    for(int item : deque) // Append the queue contents to the string
    {
        builder.append(item);
    }
    builder.append("\n"); // New line

    int temp = deque.removeFirst(); // Take the first item from the deque
    deque.addLast(temp); // And append it to the end of the deque
}

答案 2 :(得分:0)

这应该做:

for (int i = 0; i < 10; i++){
    int res = i + 1;
    for (int j = 1; j <= 10; j++){
         text.append(String.valueOf(res));
         if (res == 10) res = 0;
         res++;
     }
}

答案 3 :(得分:0)

这是一个动态解决方案,适用于min小于最大值的任何数字范围

    int min = 1;
    int max = 10;
    int count = max - min;

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i <= count; ++i) {
        for (int j = 0; j <= count; ++j) {
            sb.append(String.valueOf(((j + i) % (count + 1)) + min));
        }
        sb.append("\n");
    }

    System.out.println(sb.toString());

答案 4 :(得分:-1)

试用此代码

StringBuilder text = new StringBuilder();
StringBuilder text1 = new StringBuilder();
for (int i = 0; i <= 10; i++) {
 for (int j = 1; j <= 10; j++) {
  int respones = i;
  int respones1 = j;
  if (respones1 > respones) {
   text.append(String.valueOf(respones1));
  }
 }
 text1.append(String.valueOf(i));
 if (i > 0) {
  text.append(text1);
 }
 text.append(" ");
}
System.out.println(text.toString());

答案 5 :(得分:-1)

final int count = 10;
for (int i = 1; i <= count; i++) {
    String str = "";

    // Loop through count + i
    for (int j = i; j <= i + count -1; j++) {

    // If j is greater than count then just minus it from count
    final int currentValue = j <= count ? j : j-count;
    str  = str + currentValue;
   }
  System.out.println(str);
}

答案 6 :(得分:-1)

试试这个

StringBuilder text=new StringBuilder();
int repeat=10;
for (int index=1;index<=repeat;index++){
   int count=0;
   int temp=index;
      while (count<repeat){
         if(temp<=repeat){
            text.append(temp);
         }else {
            temp=temp-repeat;
            text.append(temp);
         }
         count++;
         temp++;
      }
      text.append(" ");
}

答案 7 :(得分:-1)

也可以按照以下方式完成:

int no = 1;
    int n = 10;
    StringBuilder sb;
    for(int i=1;i<=n;i++){
        sb = new StringBuilder();
        int cnt = 1;
        no = i;
        while(cnt<=n){
            if(no<=n){
                sb.append(no).append(" ");
                no++;
                cnt++;
            }else{
                no=(no-n);
                sb.append(no).append(" ");
                no++;
                cnt++;
            }
        }
        System.out.println(sb.toString());
    }

答案 8 :(得分:-1)

这是c#

public static void OneToTen()
{
    StringBuilder sb = new StringBuilder();
    for (int i = 1; i <= 10; i++)
    {
        sb.Clear();
        for (int j = i; j <= 10; j++)
        {
            sb.Append(j);
        }
        for (int j = 1; j < i; j++)
        {
            sb.Append(j);
        }
        Debug.WriteLine(sb.ToString());
    }

    //second solution
    Debug.WriteLine("");
    for (int i = 0; i < 10; i++)
    {
        sb.Clear();
        for (int j = 0; j < 10; j++)
        {
            sb.Append(((i+j)%10) + 1);
        }
        Debug.WriteLine(sb.ToString());
    }
    Debug.WriteLine("");
}