题解 | #动态字符串#
动态字符串
http://www.nowcoder.com/practice/e2c51a6f126b41f9be403376c7adea15
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
//write your code here.....
StringBuilder sb = new StringBuilder();
int count = 1;
for (int i = 0; i < str.length(); i += 3) {
if (str.length() % 3 == 0){
sb.append(str.substring(i,i+3)).append(",");
}else if (str.length() % 3 == 1) {
if (count == 1){
sb.append(str.substring(i,i+1)).append(",");
count = 0;
continue;
}
sb.append(str.substring(i-2,i+1)).append(",");
}else if (str.length() % 3 == 2) {
if (count == 1){
sb.append(str.substring(i,i+2)).append(",");
count = 0;
continue;
}
sb.append(str.substring(i-1,i+2)).append(",");
}
}
sb = sb.deleteCharAt(sb.length() - 1);
System.out.println(sb);
}
}