猿问

参考上述代码,预期结果=“ECTSTRI”但实际结果=“ECTTRI”

给定一个新字符串作为输入,创建一个新字符串,其中半个字母首先出现。如果字符串的长度为奇数,则中间字符保留在其位置。


我能够打印具有偶数字符串长度的所需字符串,但遇到奇数长度字符串的一些问题,因为在奇数字符串长度的情况下,中间字符应该在它自己的位置。这个怎么做?


String str = "TRISECT";

int len = str.length();

int halflen = len / 2;

String newstr = "";

String zstr = "";

if(len%2==0){

    for (int i = 0; i < halflen; i++) {

        char ch = str.charAt(i);

        newstr = newstr + ch;

    }

    for (int j = halflen; j < len; j++) {

        char ch = str.charAt(j);

        zstr = zstr + ch;

    }

    System.out.println(zstr + newstr);

}else{

    for (int i = 0; i < halflen; i++) {

        char ch = str.charAt(i);

        newstr = newstr + ch;

    }

    for (int j = halflen + 1; j < len; j++) {

        char ch = str.charAt(j);

        zstr = zstr + ch;

    }

    System.out.println(zstr + newstr);

}


慕慕森
浏览 139回答 3
3回答

大话西游666

如果你添加&nbsp;&nbsp;&nbsp;System.out.println(zstr+str.charAt(halflen)+newstr);对于满足奇数长度条件的印刷品,它应该可以解决问题。发生的事情是,由于您将字符串的一半长度分成两半(并将该值保存在 halflen 变量中),因此您的 for 不会捕获位于 halflen 位置的字符。

哆啦的时光机

尝试这个&nbsp; &nbsp; String str = "TRISECT";&nbsp; &nbsp; int len = str.length();&nbsp; &nbsp; int halflen = len / 2;&nbsp; &nbsp; String newstr = "";&nbsp; &nbsp; String zstr = "";&nbsp; &nbsp; if(len%2==0){&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < halflen; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char ch = str.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newstr = newstr + ch;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int j = halflen; j < len; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char ch = str.charAt(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zstr = zstr + ch;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(zstr + newstr);&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < halflen; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char ch = str.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newstr = newstr + ch;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; char middle;&nbsp; &nbsp; &nbsp; &nbsp; middle = str.charAt(halflen);&nbsp; &nbsp; &nbsp; &nbsp; for (int j = halflen + 1; j < len; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char ch = str.charAt(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zstr = zstr + ch;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(zstr +middle+ newstr);&nbsp; &nbsp; }

收到一只叮咚

这应该可以解决问题String str = "TRISECT";&nbsp; &nbsp; &nbsp; &nbsp; int len = str.length();&nbsp; &nbsp; &nbsp; &nbsp; int halflen = len / 2;&nbsp; &nbsp; &nbsp; &nbsp; String newstr = "";&nbsp; &nbsp; &nbsp; &nbsp; String zstr = "";&nbsp; &nbsp; &nbsp; &nbsp; if(len%2==0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < halflen; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char ch = str.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newstr = newstr + ch;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = halflen; j < len; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char ch = str.charAt(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zstr = zstr + ch;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(zstr + newstr);&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < halflen; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char ch = str.charAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newstr = newstr + ch;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char middle = str.charAt(halflen);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = halflen+1; j < len; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char ch = str.charAt(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zstr = zstr + ch;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(zstr +middle+ newstr);&nbsp; &nbsp; &nbsp; &nbsp; }在您的代码中,您将中间字符留在后面。
随时随地看视频慕课网APP

相关分类

Java
我要回答