2

在这里开始 CS 学生,试图掌握循环。我的任务是获取一个 String ,这样如果s = 'abcd',程序将打印:'a,b,c,d,ab,bc,cd,abc,bcd,abcd'。显然,随着循环的迭代,begin索引和索引会发生变化——<code>(0,1),(1,2),(2,3),(3,4)。end没关系,我可以打印:'a,b,c,d'. 但是我如何控制它,让它从那个到(0,2),(1,3),(2,4)然后(0,3),(1,4),到最后(0,4)?这就是我难住的地方。感谢您的帮助,这是我的代码:

void printSubstring()
{
    int len = s.length();

    for (int i=0;i<len;i++)
    {
        for (int k=0;k<len;k++)
           System.out.print(s.substring(k,k+1)+", "); 
    }
    System.out.println();
}
4

4 回答 4

7
for (int i=0;i<len;i++)
{
    for (int k=0;k<len-i;k++)
       System.out.print(s.substring(k,k+i+1)+","); 
}

这作为你的 for 结构应该可以工作。

在每个步骤内部循环中,要打印的字符串数会减少(使用 -i)。并且字符串的长度增加(在子字符串中使用+i)

经测试:abcdefghijklmnopqrstuvwxyz

输出:

a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,ab,bc,cd,de,ef,fg,gh,hi,ij,jk,kl,lm,mn,no,op,pq,qr,rs,st,tu,uv,vw,wx,xy,yz,abc,bcd,cde,def,efg,fgh,ghi,hij,ijk,jkl,klm,lmn,mno,nop,opq,pqr,qrs,rst,stu,tuv,uvw,vwx,wxy,xyz,abcd,bcde,cdef,defg,efgh,fghi,ghij,hijk,ijkl,jklm,klmn,lmno,mnop,nopq,opqr,pqrs,qrst,rstu,stuv,tuvw,uvwx,vwxy,wxyz,abcde,bcdef,cdefg,defgh,efghi,fghij,ghijk,hijkl,ijklm,jklmn,klmno,lmnop,mnopq,nopqr,opqrs,pqrst,qrstu,rstuv,stuvw,tuvwx,uvwxy,vwxyz,abcdef,bcdefg,cdefgh,defghi,efghij,fghijk,ghijkl,hijklm,ijklmn,jklmno,klmnop,lmnopq,mnopqr,nopqrs,opqrst,pqrstu,qrstuv,rstuvw,stuvwx,tuvwxy,uvwxyz,abcdefg,bcdefgh,cdefghi,defghij,efghijk,fghijkl,ghijklm,hijklmn,ijklmno,jklmnop,klmnopq,lmnopqr,mnopqrs,nopqrst,opqrstu,pqrstuv,qrstuvw,rstuvwx,stuvwxy,tuvwxyz,abcdefgh,bcdefghi,cdefghij,defghijk,efghijkl,fghijklm,ghijklmn,hijklmno,ijklmnop,jklmnopq,klmnopqr,lmnopqrs,mnopqrst,nopqrstu,opqrstuv,pqrstuvw,qrstuvwx,rstuvwxy,stuvwxyz,abcdefghi,bcdefghij,cdefghijk,defghijkl,efghijklm,fghijklmn,ghijklmno,hijklmnop,ijklmnopq,jklmnopqr,klmnopqrs,lmnopqrst,mnopqrstu,nopqrstuv,opqrstuvw,pqrstuvwx,qrstuvwxy,rstuvwxyz,abcdefghij,bcdefghijk,cdefghijkl,defghijklm,efghijklmn,fghijklmno,ghijklmnop,hijklmnopq,ijklmnopqr,jklmnopqrs,klmnopqrst,lmnopqrstu,mnopqrstuv,nopqrstuvw,opqrstuvwx,pqrstuvwxy,qrstuvwxyz,abcdefghijk,bcdefghijkl,cdefghijklm,defghijklmn,efghijklmno,fghijklmnop,ghijklmnopq,hijklmnopqr,ijklmnopqrs,jklmnopqrst,klmnopqrstu,lmnopqrstuv,mnopqrstuvw,nopqrstuvwx,opqrstuvwxy,pqrstuvwxyz,abcdefghijkl,bcdefghijklm,cdefghijklmn,defghijklmno,efghijklmnop,fghijklmnopq,ghijklmnopqr,hijklmnopqrs,ijklmnopqrst,jklmnopqrstu,klmnopqrstuv,lmnopqrstuvw,mnopqrstuvwx,nopqrstuvwxy,opqrstuvwxyz,abcdefghijklm,bcdefghijklmn,cdefghijklmno,defghijklmnop,efghijklmnopq,fghijklmnopqr,ghijklmnopqrs,hijklmnopqrst,ijklmnopqrstu,jklmnopqrstuv,klmnopqrstuvw,lmnopqrstuvwx,mnopqrstuvwxy,nopqrstuvwxyz,abcdefghijklmn,bcdefghijklmno,cdefghijklmnop,defghijklmnopq,efghijklmnopqr,fghijklmnopqrs,ghijklmnopqrst,hijklmnopqrstu,ijklmnopqrstuv,jklmnopqrstuvw,klmnopqrstuvwx,lmnopqrstuvwxy,mnopqrstuvwxyz,abcdefghijklmno,bcdefghijklmnop,cdefghijklmnopq,defghijklmnopqr,efghijklmnopqrs,fghijklmnopqrst,ghijklmnopqrstu,hijklmnopqrstuv,ijklmnopqrstuvw,jklmnopqrstuvwx,klmnopqrstuvwxy,lmnopqrstuvwxyz,abcdefghijklmnop,bcdefghijklmnopq,cdefghijklmnopqr,defghijklmnopqrs,efghijklmnopqrst,fghijklmnopqrstu,ghijklmnopqrstuv,hijklmnopqrstuvw,ijklmnopqrstuvwx,jklmnopqrstuvwxy,klmnopqrstuvwxyz,abcdefghijklmnopq,bcdefghijklmnopqr,cdefghijklmnopqrs,defghijklmnopqrst,efghijklmnopqrstu,fghijklmnopqrstuv,ghijklmnopqrstuvw,hijklmnopqrstuvwx,ijklmnopqrstuvwxy,jklmnopqrstuvwxyz,abcdefghijklmnopqr,bcdefghijklmnopqrs,cdefghijklmnopqrst,defghijklmnopqrstu,efghijklmnopqrstuv,fghijklmnopqrstuvw,ghijklmnopqrstuvwx,hijklmnopqrstuvwxy,ijklmnopqrstuvwxyz,abcdefghijklmnopqrs,bcdefghijklmnopqrst,cdefghijklmnopqrstu,defghijklmnopqrstuv,efghijklmnopqrstuvw,fghijklmnopqrstuvwx,ghijklmnopqrstuvwxy,hijklmnopqrstuvwxyz,abcdefghijklmnopqrst,bcdefghijklmnopqrstu,cdefghijklmnopqrstuv,defghijklmnopqrstuvw,efghijklmnopqrstuvwx,fghijklmnopqrstuvwxy,ghijklmnopqrstuvwxyz,abcdefghijklmnopqrstu,bcdefghijklmnopqrstuv,cdefghijklmnopqrstuvw,defghijklmnopqrstuvwx,efghijklmnopqrstuvwxy,fghijklmnopqrstuvwxyz,abcdefghijklmnopqrstuv,bcdefghijklmnopqrstuvw,cdefghijklmnopqrstuvwx,defghijklmnopqrstuvwxy,efghijklmnopqrstuvwxyz,abcdefghijklmnopqrstuvw,bcdefghijklmnopqrstuvwx,cdefghijklmnopqrstuvwxy,defghijklmnopqrstuvwxyz,abcdefghijklmnopqrstuvwx,bcdefghijklmnopqrstuvwxy,cdefghijklmnopqrstuvwxyz,abcdefghijklmnopqrstuvwxy,bcdefghijklmnopqrstuvwxyz,abcdefghijklmnopqrstuvwxyz,
于 2013-10-23T18:59:40.953 回答
4

内循环应该是 -

for (int k=0;k<len-i;k++)
       System.out.print(s.substring(k,k+i+1)+", "); 

希望有帮助

于 2013-10-23T19:04:48.393 回答
2
void printSubstring() {

    int len = s.length();

    for (int i=1;i<len+1;i++)
    {
        for (int k=0;k<len-i;k++)
        System.out.print(s.substring(k,k+i)+", "); 
    }
    System.out.println();
}
于 2013-10-23T18:50:25.173 回答
2

你可以试试这样的

void printSubstring(String s){
  if(s == null){
    return;
  }

  for(int lenghtSubstring = 1; lenghtSubstring <= s.length(); lenghtSubstring++){
    for(int index = 0; index <= s.length() - lenghtSubstring; index++){
      System.out.print(s.substring(index, index + lenghtSubstring) + ",");
    }
  }
}
于 2013-10-23T18:59:25.217 回答