gpt4 book ai didi

java - 带子串的for循环

转载 作者:行者123 更新时间:2023-12-02 05:37:09 24 4
gpt4 key购买 nike

这里是 CS 初学者,试图掌握循环。我的任务是获取一个 String s,如果 s = 'abcd',程序将打印:'a,b,c,d,ab,bc,cd,abc, bcd,abcd'.很明显,begin 索引和 end 索引随着循环迭代而改变——(0,1),(1,2),(2,3),(3 ,4)。很好,我可以打印:'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();
}

最佳答案

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,

关于java - 带子串的for循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19549961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com