JAVA中实现加法器

1. 不带输入的加法器(在对话框中显示结果)

(1)代码实现

package TEST;

import javax.swing.JOptionPane;  //导入类

public class TEST
{
	public static void main(String args[])
	{
		int n1,n2,sum;
		n1 = 12;
		n2 = 24;
		sum = n1+n2;
		JOptionPane.showMessageDialog(null, "The sum is:"+sum,"Adding Device",JOptionPane.PLAIN_MESSAGE);
		//第1个参数:null 显示在中央
		//第2个参数:要显示的字符
		//第3个参数:标题栏信息
		//第4个参数:对话框类型
		System.exit(0);  //终结图形用户界面程序必须的
	}
}

(2)运行结果


2. 包含输入对话框的加法器

(1)代码实现
package TEST;

import javax.swing.JOptionPane;  //导入类

public class TEST
{
	public static void main(String args[])
	{
		String input_pane1,input_pane2;
		int n1,n2,sum;
		input_pane1 = JOptionPane.showInputDialog("Please input the first number");  //输入框1
		input_pane2 = JOptionPane.showInputDialog("Please input the second number"); //输入框2
		n1 = Integer.parseInt(input_pane1); //获取输入框中输入数据的整数类型
		n2 = Integer.parseInt(input_pane2);//获取输入框中输入数据的整数类型
		sum = n1+n2;
		JOptionPane.showMessageDialog(null, "The sum is: "+sum,"Adding Device",JOptionPane.PLAIN_MESSAGE);
		//第1个参数:null 显示在中央
		//第2个参数:要显示的字符
		//第3个参数:标题栏信息
		//第4个参数:对话框类型
		System.exit(0);  //终结图形用户界面程序必须的
	}
}
(2)运行结果
                                      
                                                                    
                                                                               

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值