Java 每当字符串有空格如x3.14 3.14时,它就不会考虑后空间部分,即nnRealLoad()跳过一个输入。

Java 每当字符串有空格如x3.14 3.14时,它就不会考虑后空间部分,即nnRealLoad()跳过一个输入。,java,recursion,Java,Recursion,我在.nextLine()中的初始字符串I=in.nextLine()之后添加了额外的,因为这是我在研究此问题时发现的唯一修复方法,但它对我不起作用,它仍然只存储了两个空格。另外,System.in.read()最后只有一个,这样它就不会在接受输入后向前跳 public class replacewithpi { public static void main(String[] args) { //for sample input > 2(no

我在.nextLine()中的初始
字符串I=in.nextLine()
之后添加了额外的
,因为这是我在研究此问题时发现的唯一修复方法,但它对我不起作用,它仍然只存储了两个空格。另外,
System.in.read()最后只有一个,这样它就不会在接受输入后向前跳

public class replacewithpi {

    public static void main(String[] args) {
        //for sample input
          > 2(no of string taken using string array)
          > x3.14x
                > x3.14 3.14 3.14xx
                //for the ouput was
                > xpix
                > xpi  (the latter part was skipped but why???? s.nextLine didnt solve my problem**strong text**[hackerrankquestion][1]
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();// no of trials
        String[] str = new String[n];
        for (int i = 0; i < n; i++)//input is being stored in string array
            str[i] = s.next();
        for (int i = 0; i < n; i++)
            System.out.println(replace(str[i], ""));

    }

    public static String replace(String str, String res) {
        if (str.length() == 0) {
            StringBuilder sb = new StringBuilder();
            sb.append(res);
            return sb.toString();
        } else if (str.charAt(0) == '3' && str.charAt(1) == '.' && str.charAt(2) == '1' && str.charAt(3) == '4')
            return replace(str.substring(4), res + "pi");//why cant if(str.substring(0,4).equals("3.14") be used??                                                                                                  
        else
            return replace(str.substring(1), res + str.charAt(0));
    }
}
公共类替换为pi{
公共静态void main(字符串[]args){
//用于样本输入
>2(使用字符串数组获取的字符串数)
>x3.14x
>x3.14 3.14 3.14xx
//因为输出是
>xpix
>xpi(后一部分被跳过,但为什么???s.nextLine没有解决我的问题**强文本**[hackerrankquestion][1]
扫描仪s=新的扫描仪(System.in);
int n=s.nextInt();//试验次数
字符串[]str=新字符串[n];
for(int i=0;i
如果我没记错,您将字符串作为Int之后的输入。如果您在nextLine()方法之后立即使用nextLine()方法,请记住nextLine()读取整数标记;因此,该整数输入行的最后一个换行符仍在输入缓冲区和下一个nextLine()中排队将读取整数行的剩余部分(为空)。 在代码中,只需替换该行即可
Int n=s.nextInt();

用这个
int n=Integer.parseInt(s.nextLine());


我希望这能起作用

嘿,这很有帮助,但为什么整数输入在输入缓冲区中排队?