1.类图
2.代码
三角形demo类:
package zbx_ruance4; import java.util.*; public class Demo { public static String triangle(int a,int b,int c){ String result; if( a>=1 &&a<=100&&b>=1&&b<=100&&c>=1&&c<=100){ if(a<b+c &&b<a+c&& c<a+b){ if(a==b || a==c||b==c){ if(a==b &&b==c&&a==c) result = "等边三角形"; else result = "等腰三角形"; }else if(a*a+b*b==c*c || b*b+c*c==a*a || a*a+c*c==b*b){ result = "直角三角形"; }else{ result = "一般三角形"; } }else{ result = "不构成三角形"; } }else{ result = "边的值不在范围内!"; } return result; } public static void main(String[] args) { System.out.println("设计三角形问题的程序"); Scanner sc=new Scanner(System.in); while(true) { try{ System.out.println("请输入三角形的三条边:"); int a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); System.out.println(triangle(a,b,c)); }catch(Exception e){ e.printStackTrace(); } } } public boolean Validation(String[] value){ if(value.length<3){//输入数据不是3个 return false; } try { for(int i=0;i<3;i++){ int b = Integer.valueOf(value[i]).intValue(); } } catch (NumberFormatException e){//整形转换异常 return false; } return true; } public int perimeter(int a,int b,int c){ return a+b+c; } public float triangleArea(int a,int b,int c){ float s = (float) (0.5*perimeter( a, b, c)); return (float) Math.sqrt((s*(s-a)*(s-b)*(s-c))); } }
测试窗体类:Frame
package zbx_ruance4; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import zbx_ruance4.Demo; import javax.swing.JFrame; public class Frame{ private JFrame frame; private final JLabel label = new JLabel("请输入三角形三边:"); private final JLabel lab_a = new JLabel("请输入a:"); private final JLabel lab_b = new JLabel("请输入b:"); private final JLabel lab_c = new JLabel("请输入c:"); private final JLabel result_1 = new JLabel(""); private final JLabel result_2 = new JLabel("面积是: 周长是:"); private final JTextField text_a = new JTextField(); private final JTextField text_b = new JTextField(); private final JTextField text_c = new JTextField(); private final JButton btn_ok = new JButton("确定"); private final JButton btn_clean = new JButton("清空"); private Demo tri; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { new Frame(); } catch (Exception e) { e.printStackTrace(); } } }); } public Frame() { frame = new JFrame("三角形计算程序"); frame.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width/2)-256, (Toolkit.getDefaultToolkit().getScreenSize().height/2)-192, 450, 360); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setResizable(false); label.setFont(new Font("体", Font.PLAIN, 14)); label.setBounds(24, 10, 350, 30); frame.getContentPane().add(label); result_1.setFont(new Font("宋体", Font.PLAIN, 14)); result_1.setBounds(24, 192, 350, 30); frame.getContentPane().add(result_1); result_2.setFont(new Font("宋体", Font.PLAIN, 14)); result_2.setBounds(24, 254, 350, 30); frame.getContentPane().add(result_2); lab_a.setFont(new Font("宋体", Font.PLAIN, 14)); lab_a.setBounds(60, 70, 60, 25); lab_b.setFont(new Font("宋体", Font.PLAIN, 14)); lab_b.setBounds(60, 100, 60, 25); lab_c.setFont(new Font("宋体", Font.PLAIN, 14)); lab_c.setBounds(60, 130, 60, 25); frame.getContentPane().add(lab_a); frame.getContentPane().add(lab_b); frame.getContentPane().add(lab_c); text_a.setBounds(200, 70, 60, 25); text_b.setBounds(200, 100, 60, 25); text_c.setBounds(200, 130, 60, 25); text_a.setColumns(10); text_b.setColumns(10); text_c.setColumns(10); frame.getContentPane().add(text_a); frame.getContentPane().add(text_b); frame.getContentPane().add(text_c); btn_ok.setBounds(110, 180, 95, 25); frame.getContentPane().add(btn_ok); btn_clean.setBounds(240, 180, 95, 25); frame.getContentPane().add(btn_clean); btn_ok.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0){ if(arg0.getSource()==btn_ok){ tri = new Demo(); String[] value = new String[3]; value[0] = text_a.getText(); value[1] = text_b.getText(); value[2] = text_c.getText(); if(!tri.Validation(value)){//调用验证函数捕获异常 JOptionPane.showMessageDialog(null, "请确保输入的边为整数!", "错误", JOptionPane.ERROR_MESSAGE); allClean(); } else{ int a = Integer.valueOf(value[0]).intValue(); int b = Integer.valueOf(value[1]).intValue(); int c = Integer.valueOf(value[2]).intValue(); String triangleType = tri.triangle(a,b,c); result_1.setText(triangleType); if(triangleType.equals("边的值不在范围内!")){ JOptionPane.showMessageDialog(null, "边的值不在范围内!", "错误", JOptionPane.ERROR_MESSAGE); allClean(); }else if(triangleType.equals("不能构成三角形.")){ JOptionPane.showMessageDialog(null, "不能构成三角形.", "错误", JOptionPane.ERROR_MESSAGE); allClean(); }else{ result_2.setText("面积是:"+tri.triangleArea(a, b, c)+" 周长是:"+tri.perimeter(a, b, c)); } } } } }); btn_clean.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0){ if(arg0.getSource()==btn_clean){ allClean();//qingkong } } }); } private void allClean(){ text_a.setText(""); text_b.setText(""); text_c.setText(""); result_1.setText("类型:"); result_2.setText("面积是: 周长是:"); } }
3.