你好我正在玩StringBuffer和StringBuilder,这是我写的一个小程序,以帮助我了解它是如何工作的。然而奇怪的东西与Stringbuffer没有关系,但是我的多for if语句在我的for循环中。多如果其他如果陈述
我的小码:
public class c
{
public static void main(String args[])
{
StringBuffer infixB = new StringBuffer();
String infix = "3/8";
for(int i = 0; i < infix.length(); i++)
{
infixB.append(infix.charAt(i));
}
infixB.append(')');
System.out.println("Your current infix expression: "+infixB.toString());
//go through the 'infixB' 1 position at a time
for(int i = 0; i < infixB.length(); i++)
{
if(infixB.charAt(i) == '3')
{
System.out.println("Digit 3 at: " +i);
}
else if(infixB.charAt(i) == '/')
{
System.out.println("Operator at: "+i);
}
else if(infixB.charAt(i) == '8')
{
System.out.println("Digit 8 at: "+i);
}
else if(infixB.charAt(i) == ')');
{
System.out.println(") found at: " +i);
}
}
}
}
预期的输出会是这样的:
Your current infix expression: 3/8)
Digit 3 at: 0
Operator at: 1
Digit 8 at: 2
) found at: 3
然而,世界上没有完美的圆形,所以我的输出就出来了:
Your current infix expression: 3/8)
Digit 3 at: 0
) found at: 0
Operator at: 1
) found at: 1
Digit 8 at: 2
) found at: 2
) found at: 3
正如你所看到的,出于某种原因,在我的for循环中,最后一个if语句在pre之后执行EVEN如果或者如果陈述已经被执行,则是虚伪的。
+1。我也建议使用'switch-case'来达到这个目的。 – BackSlash
这次你比较快。 :) – Zong
哈哈谢谢,它总是我看不到的简单的东西 – JFC