急!!急!!关于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));
}
} 展开
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));
}
} 展开
2个回答
展开全部
first=Double.parseDouble(firTextField.getString());
second=Double.parseDouble(secTextField.getString());
result=Double.parseDouble(resultTextField.getString());
getString(),如果firTextField、secTextField、resultTextField不为数字parseDouble就会异常,因为你要转换的是Double,是数字,不是字符串
second=Double.parseDouble(secTextField.getString());
result=Double.parseDouble(resultTextField.getString());
getString(),如果firTextField、secTextField、resultTextField不为数字parseDouble就会异常,因为你要转换的是Double,是数字,不是字符串
展开全部
把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));
}
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));
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询