请允许我问一个愚蠢的问题。我目前正在做我的教程工作,但我不明白(charcode: message)是什么意思。
public static void main(String[] args) {
final int [] message =
{82, 96, 103, 103, 27, 95, 106, 105, 96, 28};
//the secret message
final int key = 5;
//key to unlock the message
for (int charcode: message){
System.out.print((char)(charcode + key));
}
//termincate with a newline
System.out.println();
}发布于 2011-05-30 14:46:55
它被称为森林。它允许你轻松地迭代数组中的每个元素,下面的代码将是“等价的”:
for (int i = 0; i < message.length; i++)
System.out.print((char)(message[i] + key));或者:
for (int i = 0; i < message.length; i++)
{
int charcode = message[i];
System.out.print((char)(charcode + key));
}有关更多信息,请查看documentation。
发布于 2011-05-30 14:47:19
它是增强的for循环。简而言之:它遍历message数组,并在每次迭代中将下一个值赋给charcode。
它相当于
for(int $i=0; $i<message.length; $i++) {
int charcode = message[$i];
System.out.print((char)(charcode + key));
}(注意-它将计数器命名为$i只是为了表明它是隐藏的,在增强的for循环中不可用)
发布于 2011-05-30 14:47:04
for (int charcode: message){
System.out.print((char)(charcode + key));
}这会在message中的项上创建一个循环。每次通过时,charcode都被设置为数组中的当前元素,直到打印完所有项。它被称为foreach循环。
https://stackoverflow.com/questions/6173078
复制相似问题