编写一个java application实现如下功能用户通过输入对话框先后输入2个double型数据
答案:2 悬赏:20 手机版
解决时间 2021-01-03 21:07
- 提问者网友:战魂
- 2021-01-03 04:15
被除数和除数,做除法运算后消息对话框输出结果。要求除数不能为负数,为此要自定义一个异常类DivideByMinusException(表除数为负数的异常),当除数为负数时提示用户重新输入,并处理NumberFormatException。哪位大神帮忙下应该使用JAVA异常处理try。。catch来做求解
最佳答案
- 五星知识达人网友:夜余生
- 2021-01-03 05:54
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;
import org.springframework.core.NestedRuntimeException;
public class DivisorFrame extends javax.swing.JFrame {
private javax.swing.JTextField divisor;
private javax.swing.JTextField dividend;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JButton sureBtn;
public DivisorFrame(String title) {
super(title);
//初始化
initComponents();
}
private void initComponents() {
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
divisor = new javax.swing.JTextField();
dividend = new javax.swing.JTextField();
sureBtn = new javax.swing.JButton();
jLabel1.setText("除数:");
jLabel2.setText("被除数:");
sureBtn.setText("确定");
sureBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
divid(evt);
}
});
this.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(2,2));
p1.add(jLabel1);
p1.add(divisor);
p1.add(jLabel2);
p1.add(dividend);
this.add(p1,BorderLayout.NORTH);
p2.add(sureBtn,BorderLayout.CENTER);
this.add(p2,BorderLayout.CENTER);
this.setSize(600,400);
this.setVisible(true);
pack();
}// </editor-fold>
//GEN-END:initComponents
private void divid(java.awt.event.ActionEvent evt) {
double d_divisor = 0;
double d_dividend = 0;
if(divisor.getText().equals("")){
JOptionPane.showMessageDialog(this, "被除数不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
divisor.requestFocus();
return;
}
if(dividend.getText().equals("")){
JOptionPane.showMessageDialog(this, "除数不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
divisor.requestFocus();
return;
}
try {
d_divisor = Double.parseDouble(divisor.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "数据转换异常,请填写数值型数据", "系统提示", JOptionPane.WARNING_MESSAGE);
divisor.setText("");
divisor.requestFocus();
return;
}
try {
d_dividend = Double.parseDouble(dividend.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "数据转换异常,请填写数值型数据", "系统提示", JOptionPane.WARNING_MESSAGE);
dividend.setText("");
dividend.requestFocus();
return;
}
try {
if(d_divisor <0){
divisor.setText("");
divisor.requestFocus();
JOptionPane.showMessageDialog(this, "除数不能为负数,请重新输入除数", "系统提示", JOptionPane.WARNING_MESSAGE);
throw new DivideByMinusException("除数不能为负数!");
}
double result = d_divisor / d_dividend;
JOptionPane.showMessageDialog(this, d_divisor + " / " + d_dividend + " = " + result, "计算结果", JOptionPane.WARNING_MESSAGE);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "数据转换异常,请填写数值型数据", "系统提示", JOptionPane.WARNING_MESSAGE);
dividend.requestFocus();
}
}
class DivideByMinusException extends NestedRuntimeException {
public DivideByMinusException(String msg) {
super(msg);
}
}
public static void main(String [] args){
DivisorFrame df = new DivisorFrame("除法运算,异常处理");
}
}
import java.awt.GridLayout;
import javax.swing.*;
import org.springframework.core.NestedRuntimeException;
public class DivisorFrame extends javax.swing.JFrame {
private javax.swing.JTextField divisor;
private javax.swing.JTextField dividend;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JButton sureBtn;
public DivisorFrame(String title) {
super(title);
//初始化
initComponents();
}
private void initComponents() {
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
divisor = new javax.swing.JTextField();
dividend = new javax.swing.JTextField();
sureBtn = new javax.swing.JButton();
jLabel1.setText("除数:");
jLabel2.setText("被除数:");
sureBtn.setText("确定");
sureBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
divid(evt);
}
});
this.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(2,2));
p1.add(jLabel1);
p1.add(divisor);
p1.add(jLabel2);
p1.add(dividend);
this.add(p1,BorderLayout.NORTH);
p2.add(sureBtn,BorderLayout.CENTER);
this.add(p2,BorderLayout.CENTER);
this.setSize(600,400);
this.setVisible(true);
pack();
}// </editor-fold>
//GEN-END:initComponents
private void divid(java.awt.event.ActionEvent evt) {
double d_divisor = 0;
double d_dividend = 0;
if(divisor.getText().equals("")){
JOptionPane.showMessageDialog(this, "被除数不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
divisor.requestFocus();
return;
}
if(dividend.getText().equals("")){
JOptionPane.showMessageDialog(this, "除数不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
divisor.requestFocus();
return;
}
try {
d_divisor = Double.parseDouble(divisor.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "数据转换异常,请填写数值型数据", "系统提示", JOptionPane.WARNING_MESSAGE);
divisor.setText("");
divisor.requestFocus();
return;
}
try {
d_dividend = Double.parseDouble(dividend.getText());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "数据转换异常,请填写数值型数据", "系统提示", JOptionPane.WARNING_MESSAGE);
dividend.setText("");
dividend.requestFocus();
return;
}
try {
if(d_divisor <0){
divisor.setText("");
divisor.requestFocus();
JOptionPane.showMessageDialog(this, "除数不能为负数,请重新输入除数", "系统提示", JOptionPane.WARNING_MESSAGE);
throw new DivideByMinusException("除数不能为负数!");
}
double result = d_divisor / d_dividend;
JOptionPane.showMessageDialog(this, d_divisor + " / " + d_dividend + " = " + result, "计算结果", JOptionPane.WARNING_MESSAGE);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "数据转换异常,请填写数值型数据", "系统提示", JOptionPane.WARNING_MESSAGE);
dividend.requestFocus();
}
}
class DivideByMinusException extends NestedRuntimeException {
public DivideByMinusException(String msg) {
super(msg);
}
}
public static void main(String [] args){
DivisorFrame df = new DivisorFrame("除法运算,异常处理");
}
}
全部回答
- 1楼网友:猎心人
- 2021-01-03 06:29
import javax.swing.*;
class Test
{
public static double divide(double d1,double d2) throws DivideByMinusException
{
if(d2<0)
throw new DivideByMinusException("Divisor can't be minus");
if(d2==0)
throw new NumberFormatException("divide by zero");
return d1/d2;
}
public static void main(String[] args)
{
String str1=JOptionPane.showInputDialog("请输入被除数");
String str2=JOptionPane.showInputDialog("请输入除数");
double num1=Double.parseDouble(str1);
double num2=Double.parseDouble(str2);
try
{
double num3=divide(num1,num2);
System.out.println(num3);
System.exit(0);
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
}
class DivideByMinusException extends Exception
{
private static final long serialVersionUID = 1L;
public DivideByMinusException(String str)
{
super(str);
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯