代码如下:
public static void main(String[] args) {
Scanner n =new Scanner(System.in);
String n1 = n.nextLine();
char[]x1 = n1.toCharArray();
int count = 0;
int b = 0;
char c = x1[b];
while (b <= x1.length)
{
if (c == x1[b])
{
count++;
b++;
}
else
{
System.out.print((char) c+""+count);
count = 0;
b++;
c = x1[b];
}
}
}
你题目的解答代码如下:
public static void main(String[] args) {
Scanner n =new Scanner(System.in);
String n1 = n.nextLine();
char[]x1 = n1.toCharArray();
int count = 1;
int b = 1;
char c = x1[0];
while (b < x1.length)
{
if (c != x1[b])
{
System.out.print((char) c+""+count);
count = 0;
c = x1[b];
}
count++;
b++;
}
System.out.print((char) c+""+count);
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
public static void main(String[] args) {
Scanner n =new Scanner(System.in);
String n1 = n.nextLine();
char[]x1 = n1.toCharArray();
int count = 0;
int b = 0;
char c = x1[b];
//循环次数不能等于length
while (b < x1.length)
{
if (c == x1[b])
{
count++;
b++;
}
else
{
System.out.print((char) c+""+count);
//这里该为1,因为else就已经出现一次了。
count = 1;
b++;
c = x1[b];
}
}
//结束循环要再输出一次
System.out.print((char) c + "" + count);
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!