我有一个字符串数组,它看起来像这样:的Java打印某些字符从一个数组
[67, +, 12, -, 45]
我想打印出来,所以它看起来是这样的:
67 12 + 45 -
下面的代码我试图用它来做到这一点。
String[] temp = line.split(" ");
String tmp = line.replaceAll("\\s+","");
for(int i = 0; i < temp.length; i++)
{
if(isInt(temp[i]) == false)
{
expression = temp[i];
firstExp = true;
}
else if(isInt(temp[i]) == false && firstExp == true && secondExp == false)
{
System.out.print(expression);
secondExp = true;
}
else if(isInt(temp[i]) == false && firstExp == true && secondExp == true)
{
System.out.print(expression);
firstExp = false;
secondExp = false;
}
else
{
System.out.print(temp[i]);
}
}
firstExp和secondExp是检查应出现在阵列中的表达式布尔。 isInt()只是一个用来确定字符串是否是数字的方法。现在,所有这些代码并输出这样的:
671245
有一些规则来得到这个结果'67 12 + 45 -' –
基本规则是,它需要打印所有涉及到的数字首先一个表达式,然后打印表达式本身。 – Generalkidd
那么你有如下阵列:65,23,+,36, - ,45?输出是什么? –