public class sier extends Applet{//继承applet类
Label lab1=new Label("数值1:");//定义标签,并初始化
TextField input1=new TextField(10);//定义文本框
Label lab2=new Label("数值2:");
TextField input2=new TextField(10);
Button b1=new Button("+");//添加按钮
Button b2=new Button("-");
Button b3=new Button("*");
Button b4=new Button("/");
Label lab3=new Label("符号:");
int num1,num2,data1,data2,data3;
String a="",b="",c="",d="";//初始化要显示的字符串
public void init(){//方法,往画布上添加标签,文本框,按钮
add(lab1);
add(input1);
add(lab2);
add(input2);
add(lab3);
add(b1);
b1.addActionListener(new b1Listener());//添加监听功能
add(b2);
b2.addActionListener(new b2Listener());
add(b3);
b3.addActionListener(new b3Listener());
add(b4);
b4.addActionListener(new b4Listener());
}
public void paint(Graphics g){//画布上的实现输出
g.drawString("数值1的十进制:"+a, 20, 110);
g.drawString("数值2的十进制:"+b, 20, 130);
g.drawString("计算的结果为: "+c, 20, 150);
g.drawString("结果转二进制: "+d, 20, 170);
}
class b1Listener implements ActionListener{//“+”按钮事件监听
public void actionPerformed(ActionEvent arg0) {
try{
num1=Integer.parseInt(input1.getText());//读入数值
num2=Integer.parseInt(input2.getText());
data1=ten(num1);//读入的数值转化为十进制
data2=ten(num2);
data3=ten(num1)+ten(num2);//求和
a=String.valueOf(data1);//int转化为string类型,用于在画布上输出
b=String.valueOf(data2);
c=String.valueOf(data3);
d=String.valueOf(two(data3));//和转化为二进制后再转化为字符串类型
showStatus("运行成功!");
repaint();//重画
}catch(Exception e){//监听错误,并做出相应的操作
a="";
b="";
c="";
d="";
repaint();
showStatus("出错!");
}
}
}
class b2Listener implements ActionListener{//“-”按钮事件监听
public void actionPerformed(ActionEvent arg0) {
try{
num1=Integer.parseInt(input1.getText());
num2=Integer.parseInt(input2.getText());
data1=ten(num1);
data2=ten(num2);
data3=ten(num1)-ten(num2);
a=String.valueOf(data1);
b=String.valueOf(data2);
c=String.valueOf(data3);
d=String.valueOf(two(data3));
showStatus("运行成功!");
repaint();
}catch(Exception e){//监听错误,并做出相应的操作
a="";
b="";
c="";
d="";
repaint();
showStatus("出错!");
}
}
}
class b3Listener implements ActionListener{//“*”按钮事件监听
public void actionPerformed(ActionEvent arg0) {
try{
num1=Integer.parseInt(input1.getText());
num2=Integer.parseInt(input2.getText());
data1=ten(num1);
data2=ten(num2);
data3=ten(num1)*ten(num2);
a=String.valueOf(data1);
b=String.valueOf(data2);
c=String.valueOf(data3);
d=String.valueOf(two(data3));
showStatus("运行成功!");
repaint();
}catch(Exception e){//监听错误,并做出相应的操作
a="";
b="";
c="";
d="";
repaint();
showStatus("出错!");
}
}
}
class b4Listener implements ActionListener{//“/”按钮事件监听
public void actionPerformed(ActionEvent arg0) {
try{
num1=Integer.parseInt(input1.getText());
num2=Integer.parseInt(input2.getText());
data1=ten(num1);
data2=ten(num2);
data3=ten(num1)/ten(num2);
a=String.valueOf(data1);
b=String.valueOf(data2);
c=String.valueOf(data3);
d=String.valueOf(two(data3));
showStatus("运行成功!");
repaint();
}catch(Exception e){//监听错误,并做出相应的操作
a="";
b="";
c="";
d="";
repaint();
showStatus("出错!");
}
}
}
原文请找腾讯752018766辣,文-论'文.网http://www.751com.cn }
for(int i=0;i<len;i++){//将求得的位上数值乘上相应的2的次方数,而后相加即得要求是数值
sum=sum+(int) (a[i]*Math.pow(2, len-1-i));
showStatus("运行成功!");
}
return sum;
}
public int two(int x){//十进制转化为二进制的实现方法
int a[]=new int[20];
int i=0;
String str="";
while(x!=0){
a[i]=x%2;
x=x/2;
str=a[i]+str;//要字符串来实现记忆的功能,记录下每一位上的二进制数值
i++;
}
return Integer.parseInt(str);