我正在学习Java作为初学者,但我不理解这个脚本。
我有一个'看,说'的分配 谁能告诉我这是如何运作的?
public class App {
public static void main(String[] args) {
for (int k = 0, len = args.length; k < len; k++) {
int i, j;
StringBuilder preNumber = new StringBuilder("1"); // what is the use of StringBuilder?
StringBuilder AnsNumber = new StringBuilder();
int n = Integer.parseInt(args[k]);
for (i = 1; i <= n; i++) {
AnsNumber = preNumber;
int l = preNumber.length(), cnt = 1;
StringBuilder nxtNumber = new StringBuilder();
for (j = 1; j < l; j++) {
char ch = preNumber.charAt(j);
if (preNumber.charAt(j - 1) == preNumber.charAt(j)) // what does charAt(j) means?
{
cnt++;
}
else
{
nxtNumber.append(cnt);
//why '+' isnot using as concatination?
nxtNumber.append(preNumber.charAt(j - 1));
cnt = 1;
}
// System.out.println(nxtNumber);
}
nxtNumber.append(cnt);
nxtNumber.append(preNumber.charAt(j - 1));
preNumber = nxtNumber;
}
System.out.println(AnsNumber);
}
}
}
答案 0 :(得分:1)
如果您查看Java Doc。
,您可以轻松获得答案