?? j02321212.java
字號(hào):
//程序名:j02321212
//實(shí)現(xiàn)目的:求輸入的一整數(shù)和一浮點(diǎn)數(shù)的和,差,積,商,
// 平均數(shù)以及比較兩數(shù)大小輸出較大的
//作者:計(jì)算機(jī)系023班馬鵬(21號(hào))
//------------------------------------------------------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class j02321212 extends Applet implements ActionListener//定義類
{
Label inprompt=new Label("請(qǐng)輸入一個(gè)整數(shù)和一個(gè)浮點(diǎn)數(shù): ");
Label outprompt=new Label(" ");
TextField operand1=new TextField(8); //定義并初始化控件
TextField operand2=new TextField(8);
Button H_btn=new Button("兩數(shù)之和");
Button C_btn=new Button("兩數(shù)之差");
Button J_btn=new Button("兩數(shù)之積");
Button S_btn=new Button("兩數(shù)之商");
Button P_btn=new Button("求平均數(shù)");
Button M_btn=new Button("較大之?dāng)?shù)");
Button btn=new Button("重 置");
public void init()
{
setLayout(new FlowLayout());
setSize(455,200);
setBackground(Color.blue);
add(inprompt); //加入控件
add(operand1);
add(operand2);
add(btn);
add(H_btn);
add(C_btn);
add(J_btn);
add(S_btn);
add(P_btn);
add(M_btn);
add(outprompt);
btn.addActionListener(this);
H_btn.addActionListener(this); //加入事件監(jiān)聽
C_btn.addActionListener(this);
J_btn.addActionListener(this);
S_btn.addActionListener(this);
P_btn.addActionListener(this);
M_btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
double d,result; //定義中間變量
int i;
i = Integer.parseInt(operand1.getText());
d = Double.valueOf(operand2.getText()).doubleValue();
if(e.getActionCommand()=="兩數(shù)之和") //判斷并執(zhí)行相應(yīng)操作程序塊
{
result=d+(double)i;
outprompt.setText("兩數(shù)之和為:"+result);
}
if(e.getActionCommand()=="兩數(shù)之差")
{
if(d>=(double)i)
result=d-(double)i;
else
result=(double)i-d;
outprompt.setText("兩數(shù)之差為:"+result);
}
if(e.getActionCommand()=="兩數(shù)之積")
{
result=d*(double)i;
outprompt.setText("兩數(shù)之積為:"+result);
}
if(e.getActionCommand()=="兩數(shù)之商")
{
result=(double)i/d;
outprompt.setText("兩數(shù)之商為:"+result);
}
if(e.getActionCommand()=="求平均數(shù)")
{
result=(d+(double)i)/2;
outprompt.setText("兩數(shù)的平均數(shù)為:"+result);
}
if(e.getActionCommand()=="較大之?dāng)?shù)")
{
if(d>=(double)i)
result=d;
else
result=(double)i;
outprompt.setText("兩數(shù)之中較大的數(shù)為:"+result);
}
if(e.getActionCommand()=="重置")
{
operand1.setText("");
operand2.setText("");
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -