Java – 在for循环中的打印语句未打印任何内容

huangapple go评论68阅读模式
英文:

Java - Print statement in for loop not printing anything

问题

我写了一个简单的for循环来打印字符串开头的第一个字符,然后打印字符串末尾的第一个字符...等等,但是打印语句没有显示任何内容,为什么?

英文:

I wrote simple for loop to print the first character from the beginning of the string then first character from the end.. etc, but the print statement doesn't show anything, why?

public class JavaOOP {

    public static void main(String args[]){

        String string = "abca";

        for(int i=0,j=string.length()-1;i<string.length()/2+1 && j>string.length()/2+1;i++,j--){
            System.out.println(string.charAt(i)+ " " + string.charAt(j));
        }
  
    }
}

答案1

得分: 0

请注意,这里的 j 从 3 开始,条件检查在 j 大于 3 的情况下进行,所以循环不会运行。

还要注意,这两个条件可以简化为 i <= j

public class JavaOOP {
    public static void main(String[] args) {
        String string = "abcd";

        for (int i = 0, j = string.length() - 1; i <= j; i++, j--) {
            System.out.println(string.charAt(i) + " " + string.charAt(j));
        }
    }
}
英文:

Notice that j starts at 3 and the condition checks while j is greater than 3 in this case, so the loop won't run.

Also notice that the two conditions can be simplified to i &lt;= j.

abcd:
a d
b c

abcde:
a e
b d
c c
public class JavaOOP {
    public static void main(String[] args) {
        String string = &quot;abcd&quot;;

        for(int i=0, j=string.length() - 1; i &lt;= j; i++,j--) {
            System.out.println(string.charAt(i) + &quot; &quot; + string.charAt(j));
        }
    }
}

答案2

得分: 0

不需要循环。

"...打印语句为什么什么都不显示,为什么?"

这是因为for循环条件语句的第二个条件从未满足,因此循环不会迭代。

j > string.length()/2+1

您可以使用String#length的值来捕获最后一个值。

请注意,由于字符是使用基于0的索引引用的,因此最后一个字符将是长度减1。

String string = "abca";
char first = string.charAt(0);
char last = string.charAt(string.length() - 1);

System.out.println(first + " " + last);

输出

a a

您可以使用循环来递归地捕获第一个和最后一个字符。

void traverse(String string) {
    if (string.length() <= 1) return;
    System.out.println(string.charAt(0) + " " + string.charAt(string.length() - 1));
    traverse(string.substring(1, string.length() - 1));
}

示例输出,当字符串是"stack overflow"时。

s w
t o
a l
c f
k r
  e
o v
英文:

No need for the loop.

> "... the print statement doesn't show anything, why?"

This is because the second conditional statement, of the for-loop conditional statement, is never met, thus the loop never iterates.

j&gt;string.length()/2+1

You can capture the last value using the String#length value.

Keep in mind, since the characters are referenced using a 0-based index, the last character is going to be the length, minus 1.

String string = &quot;abca&quot;;
char first = string.charAt(0);
char last = string.charAt(string.length() - 1);

System.out.println(first + &quot; &quot; + last);

Output

a a

You could use a loop to capture the first and last character, recursively.

void traverse(String string) {
    if (string.length() &lt;= 1) return;
    System.out.println(string.charAt(0) + &quot; &quot; + string.charAt(string.length() - 1));
    traverse(string.substring(1, string.length() - 1));
}

Example output, when string is, "stack overflow".

s w
t o
a l
c f
k r
  e
o v

答案3

得分: -1

您甚至不需要两个变量:

String input = /* 一些输入 */;
int len = input.length();
for (int i = 0; i < len / 2; i++) {
    System.out.println(input.charAt(i) + " " + input.charAt(len - 1 - i));
}
if (len % 2 == 1) { // 考虑奇数长度字符串的中间字符
    System.out.println(" " + input.charAt(len / 2)); // 将其放在"中间"
}

本质上,您正在利用String的对称性。您需要的是距离字符串两端相同距离的字符(在索引0input.length() - 1处)。因此,i是您距离两端的距离,计算为并从0 + iinput.length() - 1 - i简化而来。

这将导致以下结果(在奇数长度字符串中不重复字符):

输入:abcde
a e
b d
c

输入:abcdef
a f
b e
c d

英文:

You don't even need two variables:

String input = /* some input */;
int len = input.length();
for (int i = 0; i &lt; len / 2; i++) {
    System.out.println(input.charAt(i) + &quot; &quot; + input.charAt(len - 1 - i));
}
if (len % 2 == 1) { //account for middle character in odd-length string
    System.out.println(&quot; &quot; + input.charAt(len / 2)); //spaces it &quot;in the middle&quot;
}

In essence, you're using the symmetry of the String. You need characters the same distance from the ends of the strings (at indices 0 and input.length() - 1). So i is your distance from the ends, calculated as and simplified from 0 + i and input.length() - 1 - i.

This results in (without repeating the characters in an odd-length string):

Input: abcde
a e
b d
 c

Input: abcdef
a f
b e
c d

huangapple
  • 本文由 发表于 2023年6月19日 00:27:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定