以下是代码:
import java.lang.*;
import java.io.*;
class string
{
public static void main(String[] args)
{
try
{
boolean go = true;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer inp = new StringBuffer(br.readLine());
System.out.println(inp.reverse());
inp.reverse();
int leng = inp.length();
inp.setLength(leng+100);
int x = 0;
StringBuffer res = inp;
William bill = new William();
res=bill.will(x+1, leng, res);
while(x<leng-1 && go)
{
if(inp.charAt(x)==' ' && go)
{
res=bill.will(x+1, leng, res);
go = bill.bob();
}
x=x+1;
}
System.out.println(res);
}
catch (IOException uhoh)
{
System.out.println("You entered something wrong.");
System.exit(1);
}
}
}
class William
{
public boolean go;
public William()
{
this.go=true;
}
public StringBuffer will(int start, int len, StringBuffer input)
{
char cur = input.charAt(start-1);
input.delete(start-1, start-1);
int x = start;
boolean happy=true;
while(x<len && happy)
{
if(x==len-2)
{
this.go=false;
input.insert(cur, x+1);
x=x+2;
happy=false;
}
else if(input.charAt(x)==' ')
{
input.insert(cur, x);
x=x+1;
happy=false;
}
else
{
x=x+1;
}
}
return input;
}
public boolean bob()
{
return this.go;
}
}
它应该返回输入的反向(它没有错误地执行)和输入改变形式的猪拉丁语。我应该知道ikel hist(“它应该看起来像这样”)。但相反,它会在最后返回原始的StringBuffer,其中包含一堆随机数。错误中的两个值得注意的模式包括随着字母数量的增加而增加的数字,以及输入短字符串时的溢出错误。
答案 0 :(得分:7)
向后有StringBuffer.insert()的参数。这是(offset, char)
答案 1 :(得分:2)
试
input.insert(x+1, cur);
代替input.insert(cur, x+1);
(对于input.insert(cur, x)
)