Gre*_*egB 1 java if-statement char
试图搜索角色的StringBuffer外观_.我的代码在下面,但是当我运行时,我得到Match了每个数字的打印StringBuffer.
不知道它是否有所作为,但我在学习网站上运行此代码,因此没有纯Java调试器.我检查了标准的转义字符,这不是其中之一,所以我没有想法.
StringBuffer buffer = new StringBuffer(functionName);
System.out.println(buffer);
char compare='_';
char x;
for(int i=0;i<buffer.length();i++){
x = buffer.charAt(i);
System.out.println(x);
if(x==compare);
System.out.println("Match");
}
Run Code Online (Sandbox Code Playgroud)
想什么?
你可以;在条件之后删除
if (x == compare) {
System.out.println("Match");
}
Run Code Online (Sandbox Code Playgroud)
对于你的情况,它看起来像
if (x == compare); // if `x` and `compare` are equal, we do nothing
System.out.println("Match"); // it prints out `Match` regardless of the condition
Run Code Online (Sandbox Code Playgroud)