各位大哥大姐关于string字符串的处理。 比如有一字符串(1*2)+3/4+5^2,如何分离字符,并实现运算的。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package com.self;public class Ex2 { public static void main(String[] args) { String str="(1*2)+3/4+5^2"; char [] ch=str.toCharArray(); for(int i=0;i<ch.length;i++) { System.out.print(ch[i]+" "); } System.out.println(); System.out.println("str="+((1*2)+3/4.0+Math.pow(5, 2))); } }这个简单,我来,呵呵~~~ package com.self;public class Ex2 { public static void main(String[] args) { String str="(1*2)+3/4+5^2"; char [] ch=str.toCharArray(); for(int i=0;i<ch.length;i++) { System.out.print(ch[i]+" "); } System.out.println(); System.out.println("str="+((1*2)+3/4.0+Math.pow(5, 2))); } } [code=Java][package com.self;public class Ex2 { public static void main(String[] args) { String str="(1*2)+3/4+5^2"; char [] ch=str.toCharArray(); for(int i=0;i<ch.length;i++) { System.out.print(ch[i]+" "); } System.out.println(); System.out.println("str="+((1*2)+3/4.0+Math.pow(5, 2))); } }] String类中的subString()和indexOf() 方法来做!具体自己想想想,要是还做不出来,就给留言! 用jdk1.6编译执行import javax.script.*;public class Test { public static void main(String[] args) throws Exception { String exp = "(1*2)+3/4+5^2"; ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("javascript"); Object result = engine.eval(exp); System.out.println("结果类型:" + result.getClass() + ",计算结果:" + result); }} 这一个表达式,还可以用jexl来执行http://commons.apache.org/jexl/index.html 没太理解楼主的意思,如果楼主想将你的表达式分离的话直接用toArray方法生成一个char数据就好了,如果要是想计算结果的话,那你就直接将^2换成一个幂乘函数就可以了,java有相应的函数的。。 如何跳出main方法里的循环啊? JasperException异常 新手请教个关于类的方法的问题 新手求教 java中有popen()函数吗? 请教:不返回结果,当让参数数值改变?如何实现? 关于父类跟子类之间的转换问题,能人请进... 请问如何使用Log4j实现不同Client端用户写Log信息到服务器上不同的Log文件中? scjp题,帮忙解释一下为什么? 如何避免socket通信阻塞??? 请教一下,我对接口这样理解对不对: 用InetAddress获取本地IP
{
public static void main(String[] args)
{
String str="(1*2)+3/4+5^2";
char [] ch=str.toCharArray();
for(int i=0;i<ch.length;i++)
{
System.out.print(ch[i]+" ");
}
System.out.println();
System.out.println("str="+((1*2)+3/4.0+Math.pow(5, 2)));
}
}这个简单,我来,呵呵~~~
{
public static void main(String[] args)
{
String str="(1*2)+3/4+5^2";
char [] ch=str.toCharArray();
for(int i=0;i<ch.length;i++)
{
System.out.print(ch[i]+" ");
}
System.out.println();
System.out.println("str="+((1*2)+3/4.0+Math.pow(5, 2)));
}
}
package com.self;public class Ex2
{
public static void main(String[] args)
{
String str="(1*2)+3/4+5^2";
char [] ch=str.toCharArray();
for(int i=0;i<ch.length;i++)
{
System.out.print(ch[i]+" ");
}
System.out.println();
System.out.println("str="+((1*2)+3/4.0+Math.pow(5, 2)));
}
}]
具体自己想想想,要是还做不出来,就给留言!
public class Test {
public static void main(String[] args) throws Exception {
String exp = "(1*2)+3/4+5^2";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
Object result = engine.eval(exp);
System.out.println("结果类型:" + result.getClass() + ",计算结果:" + result);
}
}