请允许我提一个愚蠢的问题.我目前正在做我的教程工作,但我没有得到(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();
}
Run Code Online (Sandbox Code Playgroud)
它被称为foreach.它允许您轻松迭代数组中的每个元素,下面的代码将是'equivalant':
for (int i = 0; i < message.length; i++)
System.out.print((char)(message[i] + key));
Run Code Online (Sandbox Code Playgroud)
要么:
for (int i = 0; i < message.length; i++)
{
int charcode = message[i];
System.out.print((char)(charcode + key));
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看文档.