0

私の課題は、C2O3M1P1R1E1S10 などの文字列を受け取り、それを CCOOOMPRESSSSSSSSSS に解凍するプログラムを作成することです。ただし、プログラムで 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);
    }

  }
}

これは実際にはより大きなプログラムの一部にすぎないため、フォーマットの一部が間違っている場合は申し訳ありません.

4

1 に答える 1