急!!急!!关于J2ME ,一个简单的“计算器”程序代码,虚拟机运行有问题,请教高手

importjavax.microedition.lcdui.*;importjavax.microedition.midlet.MIDlet;importjavax.m... import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class ComputerMIDlet extends MIDlet implements CommandListener {
private Display display=Display.getDisplay(this);
private Form form=new Form("calculation");
private TextField firTextField=new TextField("first","",10,TextField.DECIMAL);
private TextField secTextField=new TextField("second","",10,TextField.DECIMAL);
private ChoiceGroup signChoiceGroup =new ChoiceGroup("sign",ChoiceGroup.POPUP,new String[]{"add","min","mul","div"},null);
private TextField resultTextField=new TextField("result","",10,TextField.DECIMAL);
private Command calaCommand=new Command("cala",Command.ITEM,1);

public ComputerMIDlet() {
form.append(firTextField);
form.append(secTextField);
form.append(signChoiceGroup);
form.append(resultTextField);
form.addCommand(calaCommand);
form.setCommandListener(this);
display.setCurrent(form);

// TODO Auto-generated constructor stub
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

}

protected void pauseApp() {

}

protected void startApp() throws MIDletStateChangeException {

}

public void commandAction(Command c, Displayable d) {
double first,second,result;
first=Double.parseDouble(firTextField.getString());
second=Double.parseDouble(secTextField.getString());
result=Double.parseDouble(resultTextField.getString());
if(c==calaCommand){
if (signChoiceGroup.getSelectedIndex()==0){
result=first+second;
}else if(signChoiceGroup.getSelectedIndex()==1){
result=first-second;
}else if(signChoiceGroup.getSelectedIndex()==2){
result=first*second;
}else{
result=first/second;
}
}
resultTextField.setString(Double.toString(result));
}

}
展开
 我来答
暗夜幼鹰
2010-11-08
知道答主
回答量:11
采纳率:0%
帮助的人:0
展开全部
first=Double.parseDouble(firTextField.getString());
second=Double.parseDouble(secTextField.getString());
result=Double.parseDouble(resultTextField.getString());

getString(),如果firTextField、secTextField、resultTextField不为数字parseDouble就会异常,因为你要转换的是Double,是数字,不是字符串
EX某某
2010-11-05
知道答主
回答量:4
采纳率:0%
帮助的人:2.9万
展开全部
把commandAction改一下
public void commandAction(Command c, Displayable d) {
double first,second,result = 0;
try {
first=Double.parseDouble(firTextField.getString());
second=Double.parseDouble(secTextField.getString());
//result=Double.parseDouble(resultTextField.getString());
} catch (Exception e) {
return;
// TODO: handle exception
}
if(c==calaCommand){
if (signChoiceGroup.getSelectedIndex()==0){
result=first+second;
}else if(signChoiceGroup.getSelectedIndex()==1){
result=first-second;
}else if(signChoiceGroup.getSelectedIndex()==2){
result=first*second;
}else{
result=first/second;
}
}
resultTextField.setString(Double.toString(result));

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式