-4
我正在學習Java作爲初學者,但我不明白這個腳本。此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);
}
}
}
你可以從Javadocs分別獲得'StringBuilder'和'String'的前兩個答案。 –
您是否閱讀過Java DOC?請再讀一遍。 – Ajit