为什么输出在此Java程序中对于相同的输入而变化?

时间:2016-04-28 09:28:33

标签: java

package mk;

import java.util.*;
import java.lang.*;

public class My {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String a,b="mni nk";
        Scanner in=new Scanner(System.in);
        a=in.next();
        System.out.println(a.replaceAll(" ", ""));
        System.out.println(b.replaceAll(" ", ""));
        if(a.replaceAll(" ","").equalsIgnoreCase(b.replaceAll(" ",""))) 

            System.out.println("True");

}
}

输出:

mni nk
mni
mnink
Picked up _JAVA_OPTIONS: -Xmx512M

1 个答案:

答案 0 :(得分:1)

使用nextLine()代替next()next()只读取一个令牌,因此a的值为"mni"(如输出中所示),而不是"mni nk"