如何在 Java 中迭代字符循环计数器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19369415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to I iterate a character loop counter in Java?
提问by user2803311
The problem states - "Write a for loop that iterates a character loop counter from 'c' to 'h', inclusive of both characters, that print the character loop counter each time with System.out.print(). Outside (and after) the loop, terminate the line with an empty System.out.println()."
问题说明 - “编写一个 for 循环,将字符循环计数器从 'c' 迭代到 'h',包括两个字符,每次使用 System.out.print() 打印字符循环计数器。外部(和之后) ) 循环,以空 System.out.println() 终止该行。”
int x =1;
for (char y = 'c'; y>=2 && y<=7; y++) {
System.out.println(y);
x++;
}
System.out.println();
回答by nanofarad
Chars can be compared directly to chars. This is notthe case for strings.
字符可以直接与字符进行比较。这不是字符串的情况。
for(char y = 'c'; y <= 'h'; y++){
System.out.print(y);
}
System.out.println();
I changed a println to a print due to the instruction:
由于指令,我将 println 更改为打印:
that print the character loop counter each time with System.out.print()
每次使用 System.out.print() 打印字符循环计数器