请问,定义的这个简单计算器,需要判断输入数据不能超出int 的最大值,不用BigInteger去接收,怎么判断?
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;
import javax.swing.*;
public class Close {
public static void main(String[] args) {
final JFrame jf =new JFrame("计算器");
jf.setBounds(200, 200, 200, 200);
jf.setResizable(false);
final JTextField jt1= new JTextField(11);
final JTextField jt2= new JTextField(11);
final JTextField jt3= new JTextField(11);
JButton jb=new JButton(" = ");
JLabel jl= new JLabel(" + ");
jf.setLayout(new FlowLayout());
/*jf.addWindowListener(new WindowAdapter() {
//void windowClosing(WindowEvent e)窗口正处在关闭过程中时调用。
public void windowClosing(WindowEvent e){
System.exit(1);
}
}) ;*/
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1=jt1.getText();
String s2=jt2.getText();
if(isNum(s1) &&isNum(s2)){
jf.setTitle("计算器");
//如果这里用Integer来判断是否溢出怎么判断?
BigInteger i1=new BigInteger(s1);
BigInteger i2=new BigInteger(s2);
jt3.setText(i1.add(i2) + "");
}else{
jf.setTitle("sorry,请输入整数数值型。");
}
}
});
jf.add(jt1);
jf.add(jl);
jf.add(jt2);
jf.add(jb);
jf.add(jt3);
jf.pack();
jf.setVisible(true);
}
//判断输入的是否是数值型。
public static boolean isNum(String s){
char[] ch =s.toCharArray();
for (char c : ch) {
if(!Character.isDigit(c)){
return false;
}
}
return true;
}
} |