我的任务是编写一个程序,它将接受一个字符串,例如 C2O3M1P1R1E1S10,并将其解压缩为 COOOMPRESSSSSSSSSS。我无法让程序正确解析 2 位数字。这就是我所拥有的。我知道我很接近。
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
int index = 0;
int numReps = 0;
char nextChar = ' ';
while (index < input.length())
{
char c = input.charAt(index);
if (!Character.isDigit(c))
{
nextChar = c;
index++;
}
else
{
while (Character.isDigit(c))
{
int temp = Integer.parseInt(""+c);
numReps = (numReps*10)+temp;
index++;
System.out.print(nextChar);
if (index >= input.length()) break;
c = input.charAt(index);
}
}
}
这实际上只是更大程序的一部分,因此如果某些格式不正确,我深表歉意。