⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j02321212.java

📁 实现整数和浮点数的加
💻 JAVA
字号:
//程序名:j02321212
//实现目的:求输入的一整数和一浮点数的和,差,积,商,
//            平均数以及比较两数大小输出较大的
//作者:计算机系023班马鹏(21号)
//------------------------------------------------------

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class j02321212 extends Applet implements ActionListener//定义类
{
	Label inprompt=new Label("请输入一个整数和一个浮点数:  ");
	Label outprompt=new Label("                                         ");
	TextField operand1=new TextField(8);    //定义并初始化控件
	TextField operand2=new TextField(8);
	Button H_btn=new Button("两数之和");
	Button C_btn=new Button("两数之差");
	Button J_btn=new Button("两数之积");
	Button S_btn=new Button("两数之商");
	Button P_btn=new Button("求平均数");
	Button M_btn=new Button("较大之数");
    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);    //加入事件监听
		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()=="两数之和")    //判断并执行相应操作程序块
	 {
		 result=d+(double)i;
		 outprompt.setText("两数之和为:"+result);
	 }
	 if(e.getActionCommand()=="两数之差")
	 {
		 if(d>=(double)i)
		 	 result=d-(double)i;
	      else
	         result=(double)i-d;
	 	outprompt.setText("两数之差为:"+result);
	 }
	 if(e.getActionCommand()=="两数之积")
	 {
	 		 result=d*(double)i;
	 		 outprompt.setText("两数之积为:"+result);
	 }
	 if(e.getActionCommand()=="两数之商")
	 {
	 		 result=(double)i/d;
	 		 outprompt.setText("两数之商为:"+result);
	 }
	 if(e.getActionCommand()=="求平均数")
	 {
			 result=(d+(double)i)/2;
			 outprompt.setText("两数的平均数为:"+result);
	 }
	 if(e.getActionCommand()=="较大之数")
	 {
		 if(d>=(double)i)
		     result=d;
		   else
		       result=(double)i;
		outprompt.setText("两数之中较大的数为:"+result);
	 }
	 if(e.getActionCommand()=="重置")
	 {
		operand1.setText("");
		operand2.setText("");
	  }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -