You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java实现动态字符串循环移位生成n个循环字符串

问题需求

我有一个内容和长度均动态变化的数字字符串,例如"13579"、"12345789"等。若字符串长度为n,需要基于它生成n个循环移位的字符串。以字符串"13579"为例,需生成如下5个从不同索引开始的结果:

13579
35791
57913
79135
91357

请问如何使用通用循环实现该功能,适配所有长度的字符串?

我尝试的代码(存在问题)

目前卡在最后一个while循环,每次操作都会触发java.lang.StringIndexOutOfBoundsException异常,原因是索引超出字符串长度或变为负数:

public class Pattern28 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc= new Scanner(System.in);
    int n= sc.nextInt();
    int i=1;
    int odd=1;
    String sNum="";
    while(i<=n) {
        int j=1;
        if(i==1) {
        while(j<=n) {
           System.out.print(odd);
         if(i==1 && j<=n) sNum=sNum.concat(Integer.toString(odd)); //to get first row numbers in string
            odd+=2;
            j++;
        }
    }
    //System.out.println("im "+sNum);
    j=i-1;
    if(i!=1) {
        //  int k=i-1;
        while(j<=n) {
            System.out.print(sNum.charAt(j));
            j++;
        }
    }
    System.out.println();
    i++;
}
}
}

问题分析与解决方案

你的代码问题点

  1. 索引越界:Java字符串索引从0开始,循环中j<=n会访问到索引n,但字符串长度为n时最大有效索引是n-1,直接触发越界异常。
  2. 逻辑缺失:只打印了从索引j到末尾的部分,没有把开头的字符拼接回来,比如第二个字符串需要3579+1,你只输出了3579。

修正后的简洁实现

利用字符串截取拼接的方式,代码简洁且适配任意长度:

import java.util.Scanner;

public class Pattern28 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int odd = 1;
        String sNum = "";
        
        // 生成初始奇数字符串
        for (int j = 0; j < n; j++) {
            sNum += odd;
            odd += 2;
        }
        System.out.println(sNum); // 输出第一个字符串
        
        // 生成后续n-1个循环移位结果
        for (int i = 1; i < n; i++) {
            // 截取从i到末尾的部分,拼接从开头到i的部分
            String shifted = sNum.substring(i) + sNum.substring(0, i);
            System.out.println(shifted);
        }
        
        sc.close();
    }
}

纯循环实现(无substring依赖)

如果要求完全用循环实现,不依赖字符串截取方法,可以用数组+取模运算实现循环索引:

import java.util.Scanner;

public class Pattern28 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int odd = 1;
        char[] numChars = new char[n];
        
        // 初始化字符数组存储数字
        for (int j = 0; j < n; j++) {
            numChars[j] = (char) ('0' + odd);
            odd += 2;
        }
        
        // 循环生成n个移位结果
        for (int start = 0; start < n; start++) {
            // 从start索引开始,循环打印n个字符
            for (int k = 0; k < n; k++) {
                // 取模运算实现循环索引,避免越界
                int currentIndex = (start + k) % n;
                System.out.print(numChars[currentIndex]);
            }
            System.out.println();
        }
        
        sc.close();
    }
}

说明

  • 第一种方法利用substring直接拼接两段字符串,代码简洁高效,适合大多数场景。
  • 第二种方法通过数组存储字符,用(start + k) % n计算循环索引,完全基于循环实现,严格适配“通用循环”的需求,支持任意长度的输入字符串。

内容的提问来源于stack exchange,提问作者Vaibhav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 02:05:34