| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1104 人关注过本帖
标题:简单的计算器程序,怎么没法运行,高人帮下忙!谢谢!
只看楼主 加入收藏
开心一科
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-10-5
收藏
 问题点数:0 回复次数:6 
简单的计算器程序,怎么没法运行,高人帮下忙!谢谢!

import java.awt.*;
import java.applet.Applet;
import java.lang.*;
public class Calculator extends Applet
{
public static void main(String args[])
{
Label lop1 = new Label("Operator 1");
Label lop2 = new Label("Operator 2");
Label lres = new Label("Result");
Label lerr = new Label("Error Message");
TextField tfop1 = new TextField(10);
TextField tfop2 = new TextField(10);
TextField tfres = new TextField(10);
TextArea taerr = new TextArea(4,15);
public void init()
{
resize(200,200);
add(lop1);
tfop1.setEditable(true);
tfop1.resize(tfop1.preferredSize());
add(tfop1);
add(lop2);
tfop2.setEditable(true);
tfop2.resize(tfop2.preferredSize());
add(tfop2);
add(lres);
tfres.setEditable(false);
tfres.resize(tfres.preferredSize());
add(tfres);
add(lerr);
taeer.setEditable(false);
taeer.resize(taeer.preferredSize());
add(taeer);
show();
}
public boolean keyDown(Event evt , int key)
{
if(evt.target == tfop2)
{
char c = (char)key;
if(c == '\n')
{
execute();
return(true);
}
return(false);
}
}
public void execute()
{
int op1,op2,result;
String opstr = tfop1.getText();
try(op1 = Interger.parseInt(opstr))
catch(NumberFormatException e )
{
taerr.appendText("Operator Format Wrong \n");
return ;
}
opstr = tfop2.getText();
try(op2 = Interger.parseInt(opstr))
catch(NumberFormatException e )
{
taerr.appendText("Operator Format Wrong \n");
return ;
}
result = op1+op2 ;
tfres.setText(Interger.toString( result ));
}
}
}

搜索更多相关主题的帖子: 计算器 高人 运行 
2006-10-20 13:45
开心一科
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-10-5
收藏
得分:0 

下面这个错误略少一点
import java.awt.*;
import java.applet.Applet;
import java.lang.*;
public class Calculator extends Applet
{
public static void main(String args[])
{
Label lop1 = new Label("Operator 1");
Label lop2 = new Label("Operator 2");
Label lres = new Label("Result");
Label lerr = new Label("Error Message");
TextField tfop1 = new TextField(10);
TextField tfop2 = new TextField(10);
TextField tfres = new TextField(10);
TextArea taerr = new TextArea(4,15);
init();
}
public void init()
{
resize(200,200);
add(lop1);
tfop1.setEditable(true);
tfop1.resize(tfop1.preferredSize());
add(tfop1);
add(lop2);
tfop2.setEditable(true);
tfop2.resize(tfop2.preferredSize());
add(tfop2);
add(lres);
tfres.setEditable(false);
tfres.resize(tfres.preferredSize());
add(tfres);
add(lerr);
taeer.setEditable(false);
taeer.resize(taeer.preferredSize());
add(taeer);
show();
}
public boolean keyDown(Event evt , int key)
{
if(evt.target == tfop2)
{
char c = (char)key;
if(c == '\n')
{
execute();
return(true);
}
return(false);
}
}
public void execute()
{
int op1,op2,result;
String opstr = tfop1.getText();
try(op1 = Interger.parseInt(opstr))
catch(NumberFormatException e )
{
taerr.appendText("Operator Format Wrong \n");
return ;
}
opstr = tfop2.getText();
try(op2 = Interger.parseInt(opstr))
catch(NumberFormatException e )
{
taerr.appendText("Operator Format Wrong \n");
return ;
}
result = op1+op2 ;
tfres.setText(Interger.toString( result ));
}
}


2006-10-20 14:09
wxhwjsw
Rank: 1
等 级:新手上路
帖 子:274
专家分:0
注 册:2006-6-15
收藏
得分:0 
。。请问你的程序是一点错误吗???我怎么复制上去到处是红线(我用的Ecplise,红线表示有错误)好多问题好像都是局部变量引起的。。建议一般都声明成全局(类)变量。。还有个人认为在main方法中不要写任何语句,在构造中写或者调用其它方法  这样可以避免一些诸如static方法不能调用non-static的方法的问题。。。。

如有什么地方说的不好还请其它高手不要见笑。。。。。

一个人的快乐,不是因为他拥有的多,而是因为他计较的少.
2006-10-20 20:38
999hits
Rank: 1
等 级:新手上路
威 望:1
帖 子:227
专家分:0
注 册:2005-10-2
收藏
得分:0 
打字错误
你的Integer,而不是Interger
还有,你不要把四个Label覆盖在一起,应该设置好他们的位置和大小,不要都用Label.preferredSize();
最后就是把你的哪些Label,TextField都放在主函数的外面,然后设置程私有的;
先改这些试试吧,错误太多有点头大

J3D的摸瞎人
2006-10-21 11:28
开心一科
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-10-5
收藏
得分:0 
这是一本教材上的程序阿!

2006-10-21 12:59
unicorn
Rank: 4
等 级:贵宾
威 望:14
帖 子:1066
专家分:0
注 册:2005-10-25
收藏
得分:0 
op2 = Integer.parseInt(opstr);

unicorn-h.spaces. ◇◆ sava-scratch.spaces.  noh enol ! pue pu!w hw u! shemle aq ll!m noh 
2006-10-21 13:04
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
以下是引用开心一科在2006-10-21 12:59:22的发言:
这是一本教材上的程序阿!

教材可能出错,也可能是你抄错了.比如楼上指出的Integer你写成Interger了


可惜不是你,陪我到最后
2006-10-21 13:13
快速回复:简单的计算器程序,怎么没法运行,高人帮下忙!谢谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018211 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved