java 一元二次方程 异常处理

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_37602174/article/details/72026888
@SuppressWarnings("serial")
public class ArgumentException extends Exception{
	public ArgumentException(String msg)
	{
		super (msg);
	}
}


import java.util.Scanner;

class Result
	{
		private static Scanner s;
	    private static double a;
	    private static double b;
	    private static double c;
		double x1;
		double x2;

	
	public  Result createResult() {
		Result t = new Result();
		return t;
		}
	
	

	public  Result resolve(double a,double b,double c)throws ArgumentException
	{
		Result r=new Result();
		createResult();
			if (a == 0)
			{
				if (b == 0)
				{
					if (c == 0)
					{
						throw new ArgumentException("参数不能全为0");
					} else 
					{
						throw new ArgumentException("该方程无解");
					}
				} else
				{
					r.x1 = r.x2 = c/b;
				}
			} else 
			{
				double delta = b * b - 4 * a * c;
				if (delta < 0)
				{
					throw new ArgumentException("该方程无解");
				} else
				{
					r.x1 = ((-b) + Math.sqrt(delta))/(2 * a);
					r.x2 = ((-b) - Math.sqrt(delta))/(2 * a);
					
				}
				
			}
		return r;
	}
	}
	

public class Resolve{
	
	private static Scanner s;
	private static double a;
	private static double b;
	private static double c;
	//public static Result createResult;
	
	
	

	public static void main(String[] args) throws ArgumentException
	{
		s = new Scanner(System.in);
		a = s.nextDouble();
		b = s.nextDouble();
		c = s.nextDouble();
		Result r=new Result();

		try
		{
			 r = r.resolve(a,b,c);
			System.out.println("x1="+r.x1);
			System.out.println("x2="+r.x2);
		}catch (ArgumentException ae)
		{
			System.out.println(ae.getMessage());
		}

	}



}


展开阅读全文

java一元二次方程的根

12-03

class SquareEquationrn private float a;rn private float b;rn private float c;rn private float rootOne;rn private float rootTwo;rn private float judge = (float) (Math.pow(this.b, 2)-(4*this.a*this.c)); //b^2-4acrn public SquareEquation(float a,float b,float c)rn this.a = a;rn this.b = b;rn this.c = c;rn rn public String getRootOne()rn if(judge>=0)rn rootOne = (float) (((-(this.b))+Math.sqrt(judge))/(2*this.a));rn return "第一个根:"+rootOne;rn elsern return "方程没有第一个根!";rn rn rn public String getRootTwo()rn if(judge>=0)rn rootTwo = (float) (((-(this.b))-Math.sqrt(judge))/(2*this.a));rn return "第二个根:"+rootTwo;rn elsern return "方程没有第二个根!";rn rn rnrnrnrnclass EquationFrame extends JFramern /**rn * rn */rn private static final long serialVersionUID = 1L;rn private Container cont = super.getContentPane();rn private JButton submit = new JButton("确定");rn private JLabel labA = new JLabel("二次项系数");rn private JLabel labB = new JLabel("一次项系数");rn private JLabel labC = new JLabel("常数项系数");rn private JTextField texta = new JTextField(5); //二次项系数rn private JTextField textb = new JTextField(5); //一次项系数rn private JTextField textc = new JTextField(5); //常数项系数rn private JTextArea textResult = new JTextArea(20,43);rn private JPanel pan1 = new JPanel();rn private JPanel pan2 = new JPanel();rn public EquationFrame()rn textResult.setEnabled(false);rn super.setTitle("一元二次方程求根");rn JScrollPane scr = new JScrollPane(pan2,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);rn submit.addActionListener(new ActionListener()rn public void actionPerformed(ActionEvent arg0) rn if(arg0.getSource()==submit)rn float a = Float.parseFloat(texta.getText());rn float b = Float.parseFloat(textb.getText());rn float c = Float.parseFloat(textc.getText());rn SquareEquation se = new SquareEquation(a,b,c);rn textResult.setText(se.getRootOne());rn textResult.append("\t\t"+se.getRootTwo());rn rn rn rn );rn super.addWindowListener(new WindowAdapter()rn public void windowClosing(WindowEvent arg0)rn System.exit(1);rn rn );rn pan1.add(labA);rn pan1.add(texta);rn pan1.add(labB);rn pan1.add(textb);rn pan1.add(labC);rn pan1.add(textc);rn pan1.add(submit);rn pan2.add(textResult);rn cont.add(scr);rn cont.add("North",pan1);rn cont.add("Center",pan2);rn super.setSize(500,300);rn super.setVisible(true);rn rnrnrnpublic class EquationMainClass rnrn public static void main(String[] args) rn new EquationFrame();rn rnrnrn方程的根总是求不对,单独检验方程表达式也没出错,小白实在找不到原因了。 论坛

没有更多推荐了,返回首页